Forum Moderators: phranque

Message Too Old, No Replies

My redirect is going to http://example.com/topic//home/user.

         

MichaelBluejay

7:44 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have this directory: /dir/topic/

I'm trying to redirect requests for </dir/topic> to </topic>. (The Rewrite in my root level .htaccess invisibly calls up </dir/topic> when just </topic> is requested. That part is fine.)

I have the Rewrite for </dir/topic> to </topic> in </topic>'s .htaccess file, because I figure that by offloading Rewrite tasks from the main .htaccess to subdirs' .htaccess will help performance some. That, and the code should be easier to maintain.

</topic>'s .htaccess has this:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /dir/topic
RewriteRule ^(.*)$ /topic/$1 [R=301,L]

But when I browse to that url, it redirects to:

http://example.com/topic//home/user/example.com/dir/topic

Curiously, if I browse to </dir/topic/> (with a final slash) then it's properly redirected to </topic>.

I've tried adding each of these, one at a time, with identical results (rewrites to that big path which includes home/user...): <RewriteBase />, <RewriteBase /topic>, <RewriteBase /topic/>, <RewriteBase /home/user/example.com/topic/>.

BTW, I hope it's obvious that I also need things like </dir/topic/page7.html> to redirect to </topic/page7.html>. That part already works fine, though.

My incompetence to solve this problem notwithstanding, I do think I did a damn fine job of explaining it. Must be all those years I spent doing tech support and tearing my hair out over poorly-explained problems.

Thanks for any help anyone can provide (though I'm betting that only jdMorgan will be able to provide any ideas).

jdMorgan

10:02 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A protocol and canonical domain should always be specified for the external redirect form of RewriteRule:

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

Jim

MichaelBluejay

1:18 am on Nov 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So there I was patting myself on the back for giving a good description of the problem, but of course I omitted something important:

I already tried using the protocol and domain and I still have the same exact problem. I even verified that the Rewrite code was executing by trying again with a *different* domain specified, and then I get similar results: http://SomeOtherDomain.com/topic//home/user/domain.com/dir/topic

Somehow it looks like my ^(.*)$ is matching the whole internal pathname. Weird.

jdMorgan

2:01 pm on Nov 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you or your hosting company installed additional code to 'fix' URLs requested with no trailing slash?

This is either a case of other code that's interfering or a mis-configured server.

Do you have other rules of this general form -- I mean other 301/302 redirects that work? If so, then look at the code above this malfunctioning code for rules that rewrite the URL but do not have an [L] flag on them. The missing [L] flag could cause this rule to see a server filepath rather than a URL as its input.

If you have no other rules, then look into using RewriteBase. I would recommend trying that first, except that the double slash in the rewritten URL argues in favor of looking for an error, rather than using this configuration tweak. It might work, but ultimately, it would be a band-aid on an underlying major wound.

Jim

MichaelBluejay

7:24 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jdMorgan, I'm loathe to disagree with you since you're the Rewrite Master but I've done some more testing and I'm not sure I'm suffering from a bad prior RewriteRule or a server misconfiguration. Here's what I've done:

I renamed the .htaccess files at the root (/) and </dir> levels so they wouldn't execute, and I made sure that my RewriteRule at </dir/topic/.htaccess> was the first RewriteRule in the file, and that it had an [L] at the end. Then I browsed to <example.com/dir/topic> and had the exact same problem I've been having all along, which seems to indicate that the cause is not interference from another RewriteRule.

Next, I moved my RewriteRule up one level, to </dir/.htaccess>, modifying it as appropriate:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /dir/topic
RewriteRule ^topic(.*)$ http://example.com/topic$1 [R=301,L]

And I disabled </dir/topic/.htaccess> so it wouldn't execute. That combination worked beautifully!

Okay, so why don't I just use that as a solution? Because </dir/.htaccess> won't execute once I reactivate my other unrelated RewriteRule's in </dir/topic/.htaccess>.

You'll note that in the modified Rewrite I took out the slash between <topic> and <$1>, but doing the same in the Rewrite at </dir/topic/.htaccess> didn't solve the problem.

Well, it's still not working right but is it good that I'm ruling more and more things out?

jdMorgan

2:08 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Okay, so why don't I just use that as a solution? Because </dir/.htaccess> won't execute once I reactivate my other unrelated RewriteRule's in </dir/topic/.htaccess>.

Not sure from reading your posts why you reached this conclusion. If higher-level .htaccess code won't execute when a lower-level .htaccess file is in place, then that probably means that RewriteOptions inherit has not been set in the server config. You can enable it in the lower-level .htaccess file if you want to try that approach.

RewriteBase can be used to remove the internal path from the rewritten URL, but again, something's not quite right here... This should not be a difficult exercise at all, and something is making it so.

Jim

MichaelBluejay

9:47 pm on Nov 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I intentionally don't use <RewriteOptions inherit> because I'm trying to improve performance. If I can prevent a page request from having to process all the <.htaccess> parents then I'd definitely like to do so.

Setting <RewriteBase /> doesn't help. The match in the RewriteRule still contains the full internal path with /home/user...etc.

I found that this works:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /dir/topic
RewriteRule ^/home/user/example.com/dir/topic(.*) /topic/$1 [R=301,L]

So it's a bit ugly but it solves my problem.

jdMorgan

12:51 am on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, OK. You server is probably set up with an "Alias" directive for that path, then. In this case, RewriteBase would be appropriate.

Something like:


RewriteBase /home/user/example.com/

will remove the necessity of matching that path in every rule.

Again, it's a good idea to provide a full canonical substitution URL when using [R=30x].

Jim

MichaelBluejay

9:24 pm on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, I'm afraid that specifying the full local path with RewriteRule gives me the same original problem (and even if I use the full canonical address in the RewriteRule). But specifying the local path in the RewriteRule works, so I guess that's my solution.