Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite IP to URL

Trying to use mod_rewrite with wildcards?

     

Vulcan315

7:05 pm on Jan 23, 2007 (gmt 0)

10+ Year Member



I'm trying to do a mod_rewrite on so when people try to come to a page at 123.123.123.123/sub_dir/page1.htm , they're automatically be redirected to www.example.com/sub_dir/page1.htm. There are about 75 pages I need to do this for, so I'm hoping a mod_rewrite will allow for wild cards... kinda like 123.123.123.123/sub_dir/* to www.domain.com/sub_dir/same final page name that was requested, but not with IP but rather domain name. Another purpose for this is to correct a possible duplicate content issue...google is determined to index those pages both ways (domain name & ip address).

Something like
RewriteCond %{HTTP_HOST} ^123.123.123.123/sub_dir/*
RewriteRule ^(.+) http://www.example.com/sub_dir/same_final_dest_requested$1 [L,R=301]

I know the syntax is whacked...I'm not the sharpest tool in the drawer on mod_rewrite. Is what I'm asking about possible with mod_rewrite?

Thanks,
Dave

[edited by: jdMorgan at 3:43 pm (utc) on Jan. 24, 2007]
[edit reason] example.com [/edit]

coopster

11:16 pm on Jan 23, 2007 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Sure, Dave. Have a look at the RewriteCond [httpd.apache.org] Directive and pay particular attention to the backreferences.

jdMorgan

2:24 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> Syntax is whacked...

Yes, but you're quite close.

> Is what I'm asking about possible with mod_rewrite?

Five examples:

In example.com/.htaccess:

 RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule ^sub_dir/(.*)$ http://www.example.com/sub_dir/$1 [R=301,L]
This does the IP redirect only for the /sub_dir/ subdirectory

In example.com/sub_dir/.htaccess:

 RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule (.*) http://www.example.com/sub_dir/$1 [R=301,L]
Because of the .htaccess file's location, this does the IP redirect only for the /sub_dir/ subdirectory

In example.com/.htaccess:

 RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
This does the IP redirect for *all* resources in the domain.

In example.com/.htaccess:

 RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
This redirects requests for *anything except* the proper domain and for *all* resources in the domain.

In example.com/httpd.conf

 RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule [b]^/([/b].*)$ http://www.example.com/$1 [R=301,L]
Same as above, showing necessary change for use in httpd.conf or conf.d

In example.com/httpd.conf

 RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.co[b]m$1[/b] [R=301,L]
Same as above, showing alternate change for use in httpd.conf or conf.d

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

Vulcan315

3:21 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



Thanks a million, folks. Your answers were brilliant, and the example:

RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule (.*) http://www.example.com/sub_dir/$1 [R=301,L]

...was EXACTLY what I needed. It worked perfectly, a quick server header check shows a 301 for the IP version of the URL, and hopefully will keep Google smiling down on me. :-)

Dave

 

Featured Threads

Hot Threads This Week

Hot Threads This Month