简体   繁体   English

301重定向不起作用(我知道另一个)

[英]301 redirect not working ( another one, I know)

Yes, I checked the related questions posted in SO, but could not find something that would help me. 是的,我检查了SO中发布的相关问题,但找不到对我有帮助的东西。

My .htaccess has plenty of redirects already, and they work fine, but this one is giving me the go around. 我的.htaccess已经有大量的重定向,它们可以正常工作,但是这个让我很忙。

I had a URL like: 我有一个类似的网址:

http://example.com/comp_all.php?vid_mod=529

which I changed to a more friendly one: 我改为一个更友好的:

http://example.com/comparatif-voiture/Audi/A4/529

In order to accomplish that I added the following rule within .htaccess: 为了做到这一点,我在.htaccess中添加了以下规则:

RewriteCond %{REQUEST_URI} ^/?comparatif-voiture [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9]+$) /comp_all.php?vid_mod=$1 [L]

And that works fine, as well. 而且效果很好。

Now, I want to have the 'old and ugly' URL still sitting out there to be redirect to the 'nice' ones. 现在,我想让“旧而丑陋的” URL仍然存在,以便重定向到“好”的URL。

I tried the following: 我尝试了以下方法:

Redirect 301 /comp_all.php?vid_mod=529 http://example.com/comparatif-voiture/Audi/A4/529

But that does not work. 但这不起作用。 It just shows the 'ugly' URL. 它只是显示“丑陋的” URL。

It does not matter whether I placed the above redirect before or after the Rewrite Rule. 我是否将上面的重定向放在重写规则之前还是之后都没关系。

You cannot use query string in Redirect directive. 您不能在Redirect指令中使用查询字符串。 You need RewriteCond in mod_rewrite like this: 您需要像这样在mod_rewrite RewriteCond

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+comp_all\.php\?vid_mod=([0-9]+) [NC]
RewriteRule ^ /comparatif-voiture/Audi/A4/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^comparatif-voiture/.+?/([0-9]+)/?$ /comp_all.php?vid_mod=$1 [L,QSA]

PS: I have also simplified your other rule. PS:我还简化了您的其他规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM