.htaccess重定向重定向301不能正常工作
问题描述:
当我尝试重定向我的页面时,没有任何反应。如果有人试图输入forum.example.com/forumdisplay.php?fid=1
我希望他们被重定向到forum.example.com/forum-1.php
。我是.htaccess
的新手,无法弄清楚。.htaccess重定向重定向301不能正常工作
.htaccess
文件:
Options -MultiViews
RewriteEngine on
Redirect 301 /forumdisplay.php?fid=([0-9]+).php /forum-$1.php
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
任何帮助将不胜感激。
答
Redirect
(mod_alias)和RewriteRule
(mod_rewrite)属于两个不同的模块。你只需要为此使用mod_rewrite。你可以试试下面的:
Options -MultiViews
RewriteEngine on
# Redirect direct requests for the "real" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^fid=([0-9]+)
RewriteRule ^forumdisplay\.php$ /forum-%1.php? [R=301,L]
# Internally rewrite back to the "real" URL
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
(Redirect
不使用正则表达式要么。)
更新:我已经附加了?
到第一RewriteRule
替代(即结束。 /forum-%1.php?
)。通过指定一个空的查询字符串,它从请求中删除查询字符串。或者,您可以使用Apache 2.4+上的QSD
标志。
谢谢。但是url显示为/forum-2.php?fid=2。有没有可能显示/forum-2.php?fid = 2部分? – Emrats
啊,是的,抱歉,我忘了这件事......我已经更新了我的答案。 – MrWhite