4

I am trying to redirect www.domain.com or domain.com to www.domain.com/temp.html

I am using the following code:

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/temp\.html" [R=301,L]

That works however I do not want to redirect IP. So if someone types in the static IP of the domain then I do not want them to be redirected to www.domain.com/temp.html

Anyone have the code to take care of this?

3 Answers 3

2

You can make that exclusion by changing your HTTP_HOST condition:

RewriteCond %{HTTP_HOST} !^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/temp\.html" [R=301,L]

or the other way around:

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

don't usethe ^.*$, that matches all, plus you can end up with a redirect loop

0

you can use this, that will work without typing the domain name itself in file, so can be used in any project.

RewriteCond %{HTTP_HOST} !^www\. [NC] [or]
RewriteCond %{HTTP_HOST} !^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.