homepage Welcome to WebmasterWorld Guest from 54.196.196.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite IP to URL
Trying to use mod_rewrite with wildcards?
Vulcan315




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

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




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

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

jdMorgan




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

> 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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved