Forum Moderators: phranque

Message Too Old, No Replies

Force WWW *AND* Redirect Domain to Subfolder

I need to be able to ensure WWW version of domain name as well as redirect

         

ambaxter

6:27 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



I am using the current code, which I found in another thread on this forum (http://www.webmasterworld.com/apache/3081740.htm)

RewriteCond $1 !^folder1/
RewriteCond %{HTTP_HOST} ^www\.domain1\.org
RewriteRule (.*) /folder1/$1 [L]

But this only works if the user includes the WWW in the URL. Is there a way to write it so that it forces WWW while still mapping the domain to the subfolder?

g1smd

6:41 pm on Feb 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The above rule is for an internal rewrite, and in order to prevent duplicate content issues it is vital that only requests for www are directly served content.

For the requested external non-www to www redirect, you need a separate set of rules, and these will need to be placed before the rewrite code.

This ensures the browser asks for the correct URL, before the rewrite actually serves the content.

ambaxter

6:54 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



Thanks for the tip. I got it to work by preceding it with

RewriteCond %{HTTP_HOST} ^domain1\.com [NC]
RewriteRule (.*) [domain1.com...] [R=301,L]

g1smd

7:17 pm on Feb 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be aware that the above code doesn't redirect www requests that include a port number.

ambaxter

7:24 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



I'm guessing here, but is this correct?


RewriteCond %{HTTP_HOST} ^domain1\.info\.?(:80)?$ [NC]
RewriteRule (.*) [domain1.info...] [R=301,L]

g1smd

7:50 pm on Feb 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The previous code did already cope with non-www requests of any type, but it didn't cope with non-canonical www requests.

The new code has exactly the same functionality. It still does not redirect any www request with a port number.

This condition allows anything that is not exactly "www.example.com" to be redirected:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$


and also ensures that blank hostname requests (in HTTP/1.0 requests) do not create an infinite loop.

[edited by: jdMorgan at 8:04 pm (utc) on Feb 7, 2011]
[edit reason] Typo -- missing negation. [/edit]