Forum Moderators: phranque

Message Too Old, No Replies

redirecting all without www to the www version

redirecting to www

         

raschidt

5:31 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



I have a site in 2 languajes, on both I want all urls with the www. then I put this in the .htaccess:

#condition like if,while
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]

#procedure if the condition satisfied
RewriteRule ^(.*)$ [site.com...] [R=301,L]

and is working fine the root site, but the spanish site is located in the folder /spanish/ and I try something like:

#condition like if,while
RewriteCond %{HTTP_HOST} ^site\.com/spanish/$ [NC]

#procedure if the condition satisfied
RewriteRule ^(.*)$ [site.com...] [R=301,L]

Also I try:

#condition like if,while
RewriteCond %{HTTP_HOST} ^site\.com\/spanish/$ [NC]

#procedure if the condition satisfied
RewriteRule ^(.*)$ [site.com\...] [R=301,L]

But not works, can you give some help on this?

thanks

jdMorgan

6:37 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should not need anything except your first rule. It will redirect all requests for the non-canonical hostname to the canonical domain, regardless of any subdirectories in the URL-path.

If you have not done so, completely flush (delete) your browser cache, and then test again.

A more-comprehensive solution, assuming that you do not use any other subdomains except for "www" is


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

This works for all subdomains, FQDN-format hostnames, appended port numbers, and casing errors, while making allowances for HTTP/1.0 clients which do not send the HTTP Host header.

Regarding your line "RewriteCond %{HTTP_HOST} ^site\.com/spanish/$ [NC]", do not try to test the URL-path in a RewriteCond checking HTTP_HOST. The HTTP_HOST variable contains only the hostname sent by the client, optionally followed by a period indicating an FQDN, and optionally followed by a port number. It will never contain any part of the URL-path unless the client is badly-broken. The URL-path should be tested in the RewriteRule pattern instead.

Jim