mod_rewrite (redirect and etc)

#1
Good day

I've installed v.4.0.1.

Firstly, I need to redirect all links www.domain.com to domain.com.
Also I really want to rewrite links with ip to links with domain name instead ip.

-------
Redirect in .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule ^(.*)$ http://yoursite.com/$1 [R=permanent,L] .

or like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
-------

Both code not working. Seems it causes a cyclic redirect.
 

auser

Super Moderator
#2
#redirect all links www.domain.com to domain.com
#Redirect in .htaccess:
RewriteEngine on
RewriteRule ^$ http://domain.com

#rewrite links with ip to links with domain name instead ip
RewriteCond %{HTTP_HOST} ^11.22.33.44$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

not sure if it actually works?
 
#3
#redirect all links www.domain.com to domain.com
#Redirect in .htaccess:
RewriteEngine on
RewriteRule ^$ http://domain.com

#rewrite links with ip to links with domain name instead ip
RewriteCond %{HTTP_HOST} ^11.22.33.44$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

not sure if it actually works?
test that before - infinite redirect )

Why this expression doesn't work?
RedirectMatch (.*) http://domain.com$1


it's a good solution
 
#5
#rewrite links with ip to links with domain name instead ip
RewriteCond %{HTTP_HOST} ^11.22.33.44$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

not sure if it actually works?
Thanks auser but I actually need not to redirect something. I just need to rewrite in all pages Ip to domain name.
 
#7
I tested,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11.22.33.44$
RewriteRule (.*) http://domain.com/$1

works as well. i.e.,
http://11.22.33.44/folder/x.php?x=1&y=2 --> http://domain.com/folder/x.php?x=1&y=2

another lesson learned: to test the redirect in Firefox 3, must clear browser cache! I read it somewhere before, Firefox 3 will remember the redirect rule in its cache.
Auser, it's not acceptable in my case:
http://11.22.33.44/folder/x.php?x=1&y=2 --> http://domain.com/folder/x.php?x=1&y=2
 

mistwang

LiteSpeed Staff
#8
to redirect from www.domain.com to domain.com, you have to match the www domain name.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

or

RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

it will take care of IP->domain.com as well.
 
#9
to redirect from www.domain.com to domain.com, you have to match the www domain name.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

or

RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

it will take care of IP->domain.com as well.
Tnx mistwang, now everything is working properly.


But can you specify why this my rule causes infinite redirect?
RewriteEngine On
RewriteRule ^([^?]*)(.*)$ http://domain.com/$1$2
 
Top