Forum Moderators: phranque
example.com/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
example.com/subdir/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/subdir/$1 [R=301,L]
www.example.com -> example.com
www.example.com/subdir/ -> example.com/subdir/
www.example.com/subdir/subdir1/ -> example.com/subdir/subdir1/
www.example.com/subdir/subdir1 -> example.com/subdir/subdir1/
Everything is working fine except for example.com/subdir (without slash) which gets redirected to http://example.com/subdir//home/domain/public_html/subdir giving a 404 error.
Any suggestions?
Milan
[edited by: jdMorgan at 3:39 pm (utc) on July 16, 2007]
[edit reason] example.com [/edit]
However, it appears that there is only a problem with the redirect in the subdirectory, and there is no need for that duplicate redirect code. So you might want to try this:
example.com/.htaccess (no change):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteOptions inherit
All of the above assumes that you're not doing any other rewrites or redirects which could interfere with the code you've shown.
Jim
I just amended the .htaccess rules to test for it and exclude it from the output.
It is a kludge(*) but I do get the correct response for all requested URLs now.
(*) If the site moves to new hosting, things may need to be edited if the physical filepath changes again.
Actually the sub directory is an independent site and it was becoming clumsy to maintain all the redirects and PHP settings in single .htaccess file at root level. So I recently separated all the directives of sub directory to its own .htaccess and soon there after I noticed the above mentioned problem.
One thing I noticed here is that as soon as I put an .htaccess file in a sub directory the canonical code in root .htaccess stops working for that sub directory onwards. Even I was wondering why the sub directory requests was not been rewritten with canonical rewrite rule.
I already tried putting all the possible file paths in RewriteBase but the problem remains.
I think I have to move all the sub directory rules back to the root .htaccess file!
I just amended the .htaccess rules to test for it and exclude it from the output.
I didn't get your point g1smd. Could you please elaborate your suggestion?
Milan
Yes. That is exactly the same situation for what I had.
.
>> Could you please elaborate your suggestion? <<
# Redirect 123.123.123.123/subsitefolder/$1/ to www.thisdomain.com/$1/
RewriteCond %{HTTP_HOST} ^123\.123\.123\.123 [NC]
RewriteRule ^(/home/user34567/public_html/subsitefolder)?(.*)$ http://www.thisdomain.com/$2 [R=301,L]
# Redirect (?-anything-?.)main-host.com/subsitefolder/$1/ to www.thisdomain.com/$1/
RewriteCond %{HTTP_HOST} ^(.*\.)?main-host\.com$ [NC]
RewriteRule ^(home/user34567/public_html/subsitefolder)?(.*)$ http://www.thisdomain.com/$2 [R=301,L]
These two rules are just two of many to fix a multitude of URLs that the site could resolve at.
Only pages at www.thisdomain.com can return "200 OK". All other related URLs return a 301 redirect.
Well I tried this
example.com/subdir/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(/home/example/public_html/subdir)?(.*)$ http://example.com/subdir/$2 [R=301,L]
Result
www.example.com -> example.com/subdir/
www.example.com/subdir -> example.com/subdir/
www.example.com/subdir/ -> example.com/subdir/
As you have noticed, every request is redirected to subdir. Did you find anything wrong in that rewrite rule?
Milan
Maybe use this instead...
RewriteCond %{HTTP_HOST} ^(.*\.)?main-host\.com$ [NC]
RewriteRule ^(/home/accountname/public_html/subdir)?(.*)$ http://example.com/$2 [R=301,L]
or this...
RewriteCond %{HTTP_HOST} ^(.*\.)?main-host\.com$ [NC]
RewriteRule ^(/home/accountname/public_html)?(.*)$ http://example.com/$2 [R=301,L]
User either:
RewriteCond %{HTTP_HOST} example\.com
RewriteCond %{HTTP_HOST} example\.com(:80)?$
RewriteCond %{HTTP_HOST} example\.com(:[0-9]{1,5})?$
Jim
www.example.com -> example.com
www.example.com/subdir -> example.com/subdir/
www.example.com/subdir/ -> example.com/subdir/
example.com/subdir -> example.com/subdir/
example.com/subdir/ -> example.com/subdir/
I cant tell you about double redirect as its a shared hosting and i cant use RewriteLog.
Milan
The good news is that it is easily fixed.
When dealing with redirects and rewrites it is very important to try your code with all input URLs that you can think of.
You need to try every combination including (but not limited to):
- www and non-www
- with and without port numbers (access via proxies may add them)
- with and without query strings (even if the site doesn't use them)
- with and without a trailing / on folders.
You need to try those all for pages that DO exist and you also need to try those for invalid (should return 404) URLs of all types too.
Reference : htaccess non-www redirect and subdir [webmasterworld.com]
Thanks to Jim and Inspired.
Milan