Welcome to WebmasterWorld Guest from 54.204.165.156

Forum Moderators: Ocean10000 & incrediBILL & phranque

Canonical redirect problem in sub directory

   
2:56 pm on Jul 16, 2007 (gmt 0)

5+ Year Member



I redirect all www requests to non-www.

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]

3:48 pm on Jul 16, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Your server configuration appears to be incorrect. There is no reason that the filepath should be injected into a canonical redirect URL. I suggest asking your host about this. In doing so, I would recommend asking them to set UseCanonicalName to "Off".

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]

example.com/subdir/.htaccess:

RewriteOptions inherit

If that does not help, see mod_rewrite's RewriteBase, and try specifying the entire "extra" filepath that appears in the path shown in the error log.

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

8:31 pm on Jul 16, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I had the same "filepath injection" problem just a few months ago.

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.

11:01 am on Jul 17, 2007 (gmt 0)

5+ Year Member



I have many internal and external redirects at root and sub directory level.

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

10:03 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



>> Actually the sub directory is an independent site <<

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.

12:42 pm on Jul 19, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Was that any use?
5:44 am on Jul 20, 2007 (gmt 0)

5+ Year Member



Oh definitely yes, but not for this case. I tried all the possible redirect rules but somehow its not working as it should be. As Jim said, I think there is some problem in server configuration and there is nothing much I can do about it while sitting on shared hosting.

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

8:15 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Your redirection target URL already includes the /subdir/ so it will be added every time.

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]

5:23 am on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Just a note... Do not end-anchor hostname patterns, or your rules will fail if the client or an intervening ISP/network proxy adds a port number!

User either:


RewriteCond %{HTTP_HOST} example\.com

or

RewriteCond %{HTTP_HOST} example\.com(:80)?$

or

RewriteCond %{HTTP_HOST} example\.com(:[0-9]{1,5})?$

or anything similar that accounts for the possibility of an appended port number.

Jim

6:26 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Cool Tip.
7:43 am on Jul 22, 2007 (gmt 0)

5+ Year Member



Thanks a lot g1smd, its working perfectly fine now.

I have the following code at 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]

Milan

4:24 pm on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Your code will work for www.example.com/subdir but what happens when you ask for example.com/subdir for example?

I assume that it will either fail, or it will do a double redirect (which is bad).

3:31 am on Jul 23, 2007 (gmt 0)

5+ Year Member



Currently everything is redirected properly.

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

11:57 am on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The Live HTTP Headers extension for Mozilla Seamonkey/Firefox will show you exactly what is going on.
12:00 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(/home/example/public_html/subdir)?(.*)$ http://example.com/subdir/$2 [R=301,L]

This code does not cater for non-www URLs.

You need to look carefully at exactly what is happening here.

1:49 pm on Jul 23, 2007 (gmt 0)

5+ Year Member



Yes its doing double redirect.

example.com/subdir -> www.example.com/subdir/ -> example.com/subdir/

I will tweak the rewrite code and let you know.

Milan

2:16 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



A double redirect (aka "redirection chain") is bad news.

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.

4:10 pm on Jul 27, 2007 (gmt 0)

5+ Year Member



Finally found the solution to double redirect problem.

Reference : htaccess non-www redirect and subdir [webmasterworld.com]

Thanks to Jim and Inspired.

Milan

 

Featured Threads

Hot Threads This Week

Hot Threads This Month