使用 mod_rewrite 重定向和重写
问完这个问题后:用于搜索查询的干净URL?我尝试了一些mod_rewrite:
RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ [NC]
RewriteRule / /s/$1? [NC,R=301,L]
RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L]
什么目标?
- 将 http://example.com/?s=query 重定向到 http://example.com/s/query
- 重写 http://example.com/s/query 到 http://example. com/?s=query
这看起来像是双重工作,但如果您仔细观察,您会看到我试图完成的任务:
- 将任何搜索查询字符串重定向到更清晰的等效项(无论是表单,还是直接输入的人) )
- 将该 URL 重写(而不是重定向)回动态查询字符串,以便我可以通过 $_GET 使用 PHP 获取它
如果我这样考虑它应该是可能的。 所以我想寻求经验丰富的模组重写者的帮助来帮助我解决这个问题。
第二个可行,但仅此而已。
After asking this question: Clean URLs for search query? I tried something with mod_rewrite:
RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ [NC]
RewriteRule / /s/$1? [NC,R=301,L]
RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L]
What's the goal?
- Redirect http://example.com/?s=query to http://example.com/s/query
- Rewrite http://example.com/s/query to http://example.com/?s=query
This looks like double work but if you take a good look you see what I try to accomplish:
- Redirect any search querystring to a cleaner equivalent (be it a form, or somebody typing it in directly)
- Rewrite (not redirect) that URL back to dynamic querystring so that I can get it with PHP via $_GET
If I think about it like this it should be possible. So I would like to seek the help of the experienced mod rewriter to help me out with this one.
Number 2 works but that's it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可行,我用一些不同的名称和目录测试了它,但对于你的情况来说应该没问题。
注意:对于 RewriteCond 中的匹配组,您必须使用
%1
而不是$1
。编辑调试(参见评论):
我的测试是:
我的apache重写是
然后询问domain.com/?doc=query显示
doc is query
对我有用。
This should work, I tested it with some different names and dirs, but that should be ok in your case.
NB: for matched group from the RewriteCond you must use
%1
not$1
.Edit for debug (see comments) :
my test is :
My apache rewrite is
Then asking for domain.com/?doc=query displays
doc is query
Works for me.