Forum Moderators: phranque

Message Too Old, No Replies

.htaccess clarity

using htaccess for non-conical urls

         

socalbrad

7:15 am on Apr 10, 2007 (gmt 0)

10+ Year Member



I know this topic has been covered a gazillion times but I have tried to implement the code from forum92/3660.htm and it works when I go to example.com but when I try w4.example.com or wwww.example.com it gives me a file not found. Did I fat finger something?

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

jdMorgan

1:08 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming you've got a space after "}" and before "!" --a character which is unfortunately removed by posting here on this forum-- then the code looks OK. But be aware that .htaccess is not the top-level control file in the server; There are one or more server configuration files above .htaccess, and these will take precedence.

I suspect you will find that the server configuration does not 'point' requests for all subddomains to the same directory as for "www" and "non-www". This may be something set up in httpd.conf or conf.d -- either manually, or using a "control panel."

In order for your .htaccess code to have any effect, requests must 'pass through it' in traversing the directory path to any files being accessed. So if the other subdomains are not 'pointed' to the directory in which your .htaccess file resides, it can have no effect.

Examining the server configuration should clarify this: If the additional subdomains are declared in a separate <VirtualHost> container and have a different <DocumentRoot> path, then your .htaccess code won't affect them.

The solution if this is the case is either to reconfigure the server DocumentRoot settings so that all requests pass through your top-level .htaccess file, or to reproduce the code in the DocumentRoot-defined directories for your additional subdomains.

I said the code looks "OK" because you don't need "[NC]" on the RewriteCond, since you want *all* non-canonical requests to be redirected. Using "[NC]" there means that case-variations of the canonical doamin *won't* be redirected, and that's "non-optimal."

Jim