301 redirect, conflicting with RewriteRule

2019-08-29 23:32发布

We did some maintenance today, and moved our web forums from /forums into the root folder of the domain.

We put in a redirect 301 in a .htaccess file:

Redirect 301 /forums/ http://www.ourforums.com/

However, we used to have some links that contained duplicate /forums folders. I.e. www.ourforums.com/forums/forums/forum.1

Obviously the redirect from above now leads to /forum.1, which odes not exist. I would like the old link to actually point to www.ourforums.com/boards/forum.1. I attempted to use something like:

RewriteRule ^/forums/forums http://www.ourforums.com/boards/ [NC,R=301,L]

Regardless of what I tried though, the Redirect seems to supersede any RewriteRules I put in the same file, regardless of whether I place them before the Redirect.

Is there any way I can somehow ensure the RewriteRule is handled before the Redirect?

3条回答
别忘想泡老子
2楼-- · 2019-08-30 00:03

This is because mod_alias (the Redirect directive) and mod_rewrite (the RewriteRule directive) is conflicting with each other in your case. Both of them play their part in the URL-file-mapping processing pipeline, where URI's get processed, rewritten, flagged, and eventually mapped to a resource and a response. You have a Redirect rule which is getting applied and the response is being flagged as a redirect. The thing about the Redirect directive is that it connects 2 path nodes together, meaning:

/forums/

is connected to

http://www.ourforums.com/

So anything below the /forums folder ends up getting redirected as well. This is why it's catching ^/forums/forums.

You can either stick to just mod_rewrite or use a RedirectMatch that excludes /forums/forums:

RewriteRule ^/forums/forums(.*)$ http://www.ourforums.com/boards$1 [NC,R=301,L]
RewriteRule ^/forums/(.*)$ http://www.ourforums.com/$1 [NC,R=301,L]

or

RedirectMatch 301 ^/forums/(?!forums)(.*)$ http://www.ourforums.com/$1
查看更多
再贱就再见
3楼-- · 2019-08-30 00:11

Manually adding redirect statements like so seems to do the trick for me:

Redirect /forums/forums/forum.1 http://www.ourforums.com/boards/forum.1
查看更多
疯言疯语
4楼-- · 2019-08-30 00:15

I had a somewhat similar problem. I was trying to add redirects from cpanel while I already had some rewrite rules written in my .htaccess file. The error I got was "No maching tag for " What I ultimately did was that kept a copy of my existing rules and cleaned the .htaccess. Then went and added all the redirects that I needed from cpanel, and then at the end put back my own rewrite rules in the end of the file. That worked for me

查看更多
登录 后发表回答