.htaccess: “redirect permanent” with fallback to a “RewriteRule”

一世执手 提交于 2019-12-22 12:43:39

问题


i have a webshop where we switch the subdirectory. From "/shop" to the root "/". Also some Product- and category-names change.

I want to redirect some specific URLs to newer ones. For Example: www.domainname.de/shop/product_abc to www.domainname.de/product_abc_v2 For this, I use the Redirect Permanent declaration in the .htaccess File.

In the next Step I want a Fallback for all other URL´s which looks like this: www.domainname.de/shop/XXXXX.

So i write this Rewrite Rule:

RewriteCond %{REQUEST_URI} ^/shop/(.*)
RewriteRule ^(.*)$ http://www.domainname.de/ [r=301,L]
RewriteRule ^(.*)/$ $1 [R=301,L]

Now my problem: I put the Rewrite Rule to the bottom of the .htaccess File so that the Redirect Permanent-Code would be used before the Rewrite Rule is used. But anyway the Rewrite Rule is always taken. Is there any solution for this problem?!

Thanks a lot!


回答1:


The solution is to use only mod_rewrite. When you mix mod_alias (Redirect Permanent) and mod_rewrite (RewriteRule) directives together, both modules will get applied to every URL, and in cases where both get applied, you end up with some unexpected behavior.

The first thing you need to do is change the Redirect to Rewrite:

Redirect Permanent /shop/product_abc /shop/product_abc_v2

to:

RewriteRule ^shop/product_abc(.*)$ /shop/product_abc_v2$1 [L,R=301]

Then you need to make sure the new rule is in the right place. The order matters with rewrite rules.



来源:https://stackoverflow.com/questions/25969658/htaccess-redirect-permanent-with-fallback-to-a-rewriterule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!