homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Canonical redirect problem in sub directory
milanmk

5+ Year Member



 
Msg#: 3395854 posted 2:56 pm on Jul 16, 2007 (gmt 0)

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]

 

jdMorgan

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



 
Msg#: 3395854 posted 3:48 pm on Jul 16, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 8:31 pm on Jul 16, 2007 (gmt 0)

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.

milanmk

5+ Year Member



 
Msg#: 3395854 posted 11:01 am on Jul 17, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 10:03 pm on Jul 17, 2007 (gmt 0)

>> 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.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 12:42 pm on Jul 19, 2007 (gmt 0)

Was that any use?

milanmk

5+ Year Member



 
Msg#: 3395854 posted 5:44 am on Jul 20, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 8:15 pm on Jul 20, 2007 (gmt 0)

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]

jdMorgan

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



 
Msg#: 3395854 posted 5:23 am on Jul 21, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 6:26 pm on Jul 21, 2007 (gmt 0)

Cool Tip.

milanmk

5+ Year Member



 
Msg#: 3395854 posted 7:43 am on Jul 22, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 4:24 pm on Jul 22, 2007 (gmt 0)

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).

milanmk

5+ Year Member



 
Msg#: 3395854 posted 3:31 am on Jul 23, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 11:57 am on Jul 23, 2007 (gmt 0)

The Live HTTP Headers extension for Mozilla Seamonkey/Firefox will show you exactly what is going on.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 12:00 pm on Jul 23, 2007 (gmt 0)

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.

milanmk

5+ Year Member



 
Msg#: 3395854 posted 1:49 pm on Jul 23, 2007 (gmt 0)

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3395854 posted 2:16 pm on Jul 23, 2007 (gmt 0)

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.

milanmk

5+ Year Member



 
Msg#: 3395854 posted 4:10 pm on Jul 27, 2007 (gmt 0)

Finally found the solution to double redirect problem.

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

Thanks to Jim and Inspired.

Milan

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved