Forum Moderators: phranque

Message Too Old, No Replies

Please help me modify htaccess to redirect IP to domain

have had huge problems in past, don't want to make mistake

         

Clark

3:13 am on Jun 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is my current .htaccess:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

I just noticed my ip address does not redirect properly...how would I add the IP Address properly?

I've seen some code to do it, but that doesn't have the above code to handle example.com->www.example.com and I don't want the commands to step on each other.

Thanks.

[edited by: jdMorgan at 4:18 am (utc) on June 11, 2007]
[edit reason] example.com [/edit]

jdMorgan

4:23 am on Jun 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The general method would be to use the [OR] flag to add another RewriteCond. So here's one way to do it:

Options +FollowSymlinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^192\.168\.10\.1
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

Clark

6:35 am on Jun 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, I assume the
#
alone on a line is the comment symbol for apache and I don't need it?

EDIT: Oh and I see you changed the [r=301,nc]
to
[r=301,L], was there a particular reason for it?

[edited by: Clark at 6:36 am (utc) on June 11, 2007]

jdMorgan

1:53 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The changes were for two reasons:

1. Best practices for readability (I type stuff in my "style" by habit).
2. So it will work properly now -- or later when you add more rules. (The [NC] flag with (only) a ".*" pattern is useless, and [L] tells the rewrite engine to stop for this pass through .htaccess if the rule is invoked.)

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

Clark

4:46 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK cool. Just checking to make sure there are no typos. Also, I see you removed the $...

OK I'm going to run it. Wish me luck!

[edited by: Clark at 4:46 am (utc) on June 12, 2007]