使用 mod_rewrite 重定向?
所以我有一个网页,根据这个参数search.php?state=AL
查询一些数据。
我想做的是在我的 .htaccess
文件中编写一条规则,该规则将:
- 将
website.com/state/AL
转换为search.php?state =AL
- 如果用户明确请求
search.php?state=AL
,则将其转换为/state/AL
我使用以下命令完成了步骤 1:
RewriteRule ^state/([A-Za-z][A-Za-z]) /search.php?state=$1 [NC]
如何完成步骤 2?我知道我必须使用 [R, NC] 来实际重写 URL,但这就是我陷入困境的地方。
编辑:不确定这是否重要,但我的网络主机迫使我出于某种原因将 RewriteBase /
添加到我的 .htaccess
文件的顶部。
So I have a webpage that queries some data based on this parameter search.php?state=AL
.
What I'm trying to do is write a rule in my .htaccess
file that will:
- translate
website.com/state/AL
intosearch.php?state=AL
- If a user specifically request
search.php?state=AL
then translate that into/state/AL
I accomplished step 1 using this:
RewriteRule ^state/([A-Za-z][A-Za-z]) /search.php?state=$1 [NC]
How can I accomplish step 2? I know I will have to use [R, NC]
to actually rewrite the URL, but thats where I'm stuck.
EDIT: Not sure if this matters but my webhost forces me form some reason to add RewriteBase /
to the top of my .htaccess
file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只想在原始请求指向
/search.php?state=
URL 的情况下执行重定向,如下所示:You want to perform the redirection only in the case that the original request was pointed to the
/search.php?state=
URL, like so:像这样的事情就可以解决问题:
Something like this would do the trick: