Forum Moderators: phranque
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).
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.
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
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?
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
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.
Something like:
RewriteBase /home/user/example.com/
Again, it's a good idea to provide a full canonical substitution URL when using [R=30x].
Jim