[英].htaccess 301 redirect not working
I have a basic CMS in PHP/MySQL where content managers can create pages to the system for public viewing. 我在PHP / MySQL中有一个基本的CMS,内容管理员可以在其中创建系统页面供公众查看。 Each page is then available at an url such as
http://www.example.com/pages.php?pid=123
Now, I want to redirect requests to http://www.example.com/pages.php?pid=123
to http://www.example.com/pages.php?pid=456
. 然后,每个页面都位于URL处,例如
http://www.example.com/pages.php?pid=123
现在,我想将请求重定向到http://www.example.com/pages.php?pid=123
到http://www.example.com/pages.php?pid=456
。
I've already removed the pid=123
page from the db but because of the cms code the site still returns a 202 when some one tries to access the page. 我已经从数据库中删除了
pid=123
页面,但是由于cms代码,当有人尝试访问该页面时,该站点仍返回202。 I thought I could use a 301 redirect in .htaccess
to make the redirect work, ie: 我以为可以在
.htaccess
使用301重定向来使重定向工作,即:
redirect 301 pages.php?pid=123 http://www.example.com/pages.php?pid=456
but this doesn't work, Apache still return 202 when trying to fetch the pid=123 page. 但这不起作用,当尝试获取pid = 123页面时,Apache仍返回202。 Also, I've tried using mod_rewrite, but it doesn't work:
另外,我尝试使用mod_rewrite,但是它不起作用:
RewriteRule ^pages.php?pid=123$ pages.php?pid=456 [R=301,L]
Any ideas what could be wrong and how I can fix the 301 redirect? 任何想法可能有什么问题,以及如何解决301重定向?
Both the Redirect
and RewriteRule
directive work just on the URL path . Redirect
和RewriteRule
指令RewriteRule
在URL路径上工作。 In mod_alias ( Redirect
directive) you can not test the query and in mod_rewrite ( RewriteRule
directive) you need an additional RewriteCond
directive : 在mod_alias(
Redirect
指令)中,您无法测试查询;在mod_rewrite( RewriteRule
指令)中,您需要一个附加的RewriteCond
指令 :
RewriteCond %{QUERY_STRING} (^|&)pid=123(&|$)
RewriteRule ^pages\.php$ /pages.php?pid=456 [R=301,L]
But it would certainly be better if your CMS can handle such redirects since it's your CMS that knows best what URLs are valid and what are not. 但是,如果CMS可以处理这样的重定向,那肯定会更好,因为您的CMS最清楚什么URL有效,什么URL不有效。
You can perform the redirect in PHP (which probably knows more about what to redirect where) using header()
. 您可以使用
header()
在PHP中执行重定向(这可能会更多地了解在何处重定向header()
。
Please note that ?
请注意
?
is a special character used by regular expressions, so your regex matches pages.phppid=123
and pages.phppid=123
. 是正则表达式使用的特殊字符,因此您的正则表达式匹配
pages.phppid=123
和pages.phppid=123
。
Even then, I don't think the query string (including the ?pid=123
part) is used in the URL handled by RewriteRule
, so you would need to use something like: 即使这样,我也不认为
RewriteRule
处理的URL中使用了查询字符串(包括?pid=123
部分),因此您将需要使用类似以下内容:
RewriteCond %{QUERY_STRING} ^pid=123$
RewriteRule ^pages.php$ pages.php?pid=456 [R=301,L]
This shouldn't work as is, but it should give you some ideas. 这不应该按原样进行,但是应该可以给您一些想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.