homepage Welcome to WebmasterWorld Guest from 54.205.144.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
capturing domain for 301 redirect
batface




msg:4599057
 10:52 am on Aug 3, 2013 (gmt 0)

The site has multiple language domains. The structure is identical same base structure with configurations for each language. Is there a way to generically capture the requesting domain and use that in the rewrite? For example:

RewriteCond %{HTTP_HOST} .
RewriteCond %{REQUEST_URI} ^/products/
RewriteCond %{THE_REQUEST} /([^/]+)/\ HTTP/
RewriteRule /$ {HTTP_HOST}/%1 [R=301,L]


http://www.example.de/products/blah/blah/pencil
to
http://www.example.de/pencil

or

http://www.example.co.uk/products/blah/blah/pencil
to
http://www.example.co.uk/pencil

 

g1smd




msg:4599061
 11:03 am on Aug 3, 2013 (gmt 0)

Yes, the rule target becomes

http://%{HTTP_HOST}/%1

Be aware this creates a problem in that non-www requests are redirected to non-www and www requests are redirected to www. This means an old request for the non-canonical hostname invokes a two-step (or more) redirection chain.

It is possible to add extra code such that all requests redirect to www in one step, but this gets messy.

Be aware that your example code redirects various URLs that always end with a trailing slash but your URLs presented as examples do not have a trailing slash.

batface




msg:4599067
 11:16 am on Aug 3, 2013 (gmt 0)

Be aware this creates a problem in that non-www requests are redirected to non-www and www requests are redirected to www. This means an old request for the non-canonical hostname invokes a two-step (or more) redirection chain.

It is possible to add extra code such that all requests redirect to www in one step, but this gets messy.


Would you suggest I implement this rule followed by a new rule redirecting any non www to www?

Be aware that your example code redirects various URLs that always end with a trailing slash but your URLs presented as examples do not have a trailing slash.


Actually this needs fixing in general as both can be used and I will remove trailing /, then implement this rule.

g1smd




msg:4599069
 12:15 pm on Aug 3, 2013 (gmt 0)

Yes, you should have a rule following this one that redirects requests for non-www to to www.

However, this results in a two-step redirection chain for non-www requests. It is better if the first rule also fixes the www at the same time as fixing whatever else it needs to fix.

You still need the general non-www to www redirect in place, as the last rule of all redirects, for all other requests not fixed by earlier rules.

lucy24




msg:4599121
 8:19 pm on Aug 3, 2013 (gmt 0)

At its simplest: can you have a pair of rules? Here assuming www. is the desired form

RewriteCond %{HTTP_HOST} ^www\.
{et cetera as given, except you don't need the . condition}

and then the identical ruleset again, minus the "www." first condition, with the target given as

http:/ /www.%{HTTP_HOST}/%1

:: wondering if it would be simpler overall to disable autolinking in this subforum :

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