Forum Moderators: phranque

Message Too Old, No Replies

subdirectory with same name as one above it

         

hooter

8:23 am on Dec 1, 2005 (gmt 0)

10+ Year Member



Hello,

I currently have an .htaccess in a directory called /catalog. There is a subdirectory under that also called catalog ie: /catalog/catalog. All my old content has been moved from the subdirectory to /catalog

My rewrite rule contained in /catalog/.htaccess looks like this:
Options +SymlinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteRule ^/catalog(.*)$ http://www.example.com$1 [R=301,L]

However if the URL is www.example.com/catalog/catalog/stuff it doesn't rewrite it to just www.example.com/catalog/stuff which is what I'm shooting for.

I've tried some different permutations:
RewriteRule ^/catalog/catalog(.*)$ http://www.example.com/catalog$1 [R=301,L]

but the resultant URL still comes out http://www.example.com/catalog/catalog/stuff

Obviously I'm missing something here :)
Any guidance would be greatly appreciated. Many thanks.

jdMorgan

2:41 pm on Dec 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In .htaccess, the path to the current directory (where *this* .htaccess file resides) is stripped.

The path to your current directory in this case is "/catalog/", so the RewriteRule won't match. Try:


RewriteRule [b]^cat[/b]alog(.*)$ http://www.example.com$1 [R=301,L]

This subtle difference between mod_rewrite code in httpd.conf and .htaccess is often overlooked, but can be seen in the Apache Guide to URL Rewriting (See link in our forum Charter).

Jim

hooter

6:04 pm on Dec 1, 2005 (gmt 0)

10+ Year Member



Hi Jim,

Many thanks for your response. If I try:
RewriteRule ^catalog(.*)$ http://www.example.com$1 [R=301,L]

and use a URL like:
www.example.com/catalog/catalog/stuff
it gets rewritten as:
www.example.com/stuff and a resultant 404.

What I need is for:
www.example.com/catalog/catalog/stuff
to be rewritten and 301'ed as:
www.example.com/catalog/stuff

Thanks and sorry for the confusion.

jdMorgan

7:16 pm on Dec 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, sorry, I picked the wrong example you posted above to modify. The problem is the identical names for subdirectory and sub-subdirectory. A check of your server access logs will likely show each /catalog/catalog request getting redirected twice: /catalog/catalog/stuff 301--> /catalog/stuff 301--> /stuff

Try:

 RewriteRule ^catalog/catalog(.*)$ http://www.example.com/catalog$1 [R=301,L] 

instead.

Jim

hooter

5:25 am on Dec 2, 2005 (gmt 0)

10+ Year Member



Hi Jim,

Well, what you suggested sure makes sense to me:
RewriteRule ^catalog/catalog(.*)$ http://www.example.com/catalog$1 [R=301,L]

However using that as my RewriteRule nothing gets rewritten ie. using this URL:
http://www.example.com/catalog/catalog/stuff
still ends up in browser location as:
http://www.example.com/catalog/catalog/stuff

And I cleared cache, closed and opened 2 different browsers (IE6 and FF 1.5), ctrl F5 to force reload of page but still no joy :)

jdMorgan

6:52 am on Dec 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you got another .htaccess in /catalog/catalog? Or does that directory still exist?

You may need to turn on


RewriteOptions inherit

in .htaccess in that subdirectory if it exists. Or just rename it temporarily for testing purposes.

(If inherit is not enabled in the server configuration, then subdirectories do not inherit the rewriterules and rewritemaps of their parent directories. Many if not most hosts turn it on by default to avoid this problem.)

Jim

hooter

2:02 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



Hi Jim,

No htaccess in /catalog/catalog
Yes the subdirectory still exists
Yes, I tried renaming it for testing purposes (/catalog/backup)

Still same results. Using the URL www.example.com/catalog/catalog/stuff results in the same URL with nothing being rewritten.

Many thanks Jim for taking the time to help me along here.

jdMorgan

2:57 pm on Dec 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This rule

RewriteRule ^catalog/catalog(.*)$ http://www.example.com/catalog$1 [R=301,L]

must be located in example.com/.htaccess in order for the pattern to match your requested URL.

Is that the case? If so, I can't tell you why it won't work, unless you have httpd.conf access. If you do have access to the server config, then turn on detailed mod_rewrite logging to see where it's going wrong.

It's really frustrating when a very simple rule won't work on somebody's server...

Jim

hooter

4:00 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



Hi Jim,

First, much appreciated for you taking the time to help. I understand your frustration as I have tried the same scenario you have outlined on my own server and it works as advertised!

Unfortunately still not working on my friends setup - he is on shared hosting so no access to httpd.conf etc. and no real access to any forensic logs to see what is really going on.

For now we've just gone with a simple htaccess file in the /catalog/catalog directory with a simple 301 redirect up one directory - not as elegant as we would've liked but it handles the situation for now.

Again, many thanks Jim.