如何从apache 301重定向中删除URL参数?

时间:2010-02-17 04:59:10

标签: apache

我正在尝试对我的网站上的重定向进行硬编码,从基于类别的URL到固定网址。

网址结构为:

http://www.mysite.com/my-category-c-17_12.html 

我正在尝试将其重定向到:

http://www.mysite.com/my-static-category.php

在我的.htaccess文件中使用以下重定向:

Redirect 301 /my-category-c-17_12.html http://www.mysite.com/my-static-category.php

我最终得到了:

http://www.mysite.com/my-static-category.php?cpath=17_12

在我的重定向下面,我还有以下一行:

RewriteRule ^(.*)-c-([0-9_]+).html$ index.php?cPath=$2&%{QUERY_STRING} 

我不确定这是不是造成它的原因。基本上我想要一种从重定向的URL中剥离参数的方法。有谁知道如何处理?谢谢!

2 个答案:

答案 0 :(得分:1)

如果您已经拥有mod_rewrite,为什么不直接使用呢?

RewriteRule ^(.*)-c-([0-9_]+).html$ $1.php [R=301,L]

答案 1 :(得分:0)

如果您不想进行全局重写(根据Ignacio Vazquez-Abrams的建议) 不要使用Redirect指令,只需使用RewriteRule。像(完整)的东西:

RewriteEngine on

# Limited redirects
RewriteCond %{REQUEST_FILENAME} my-category-c-17_12.html$ [OR,NC]
RewriteCond %{REQUEST_FILENAME} my-category-c-17_13.html$ [OR,NC]
RewriteCond %{REQUEST_FILENAME} my-category-c-17_14.html$
RewriteRule ^(.*)-c-([0-9_]+).html$ $1.php [R=301,L]

# Other rewrites
RewriteRule ^(.*)-c-([0-9_]+).html$ index.php?cPath=$2 [QSA,L]

然后,您可以向RewriteCond添加其他网址,以允许重定向更多网址。

相关问题