htaccess 重写/重定向
我在构建重写规则时遇到一些问题。我想要重写并最终重定向的网址包含一个搜索查询,如下所示:
http://www.mysite.com/pages.php?category=fruit
我想重定向它至:
http://www.mysite.com/pages.php/fruit
原始地址已不存在。我试图构建一个重写,但这并不能完全按照我希望的方式工作,
RewriteEngine on
RewriteCond %{QUERY_STRING} =category=fruit`
RewriteRule ^pages\.php$ pages.php/fruit/ [L,R=301]
任何有关
http://www.mysite.com/home/linux123/m/mysite.com/user/htdocs/pages.php/fruit/
修复重写规则构建的建议都会很棒。提前致谢。
I am having some problems constructing a rewrite rule. The url I want to rewrite and ultimately redirect has a search query in it and looks like this:
http://www.mysite.com/pages.php?category=fruit
I would like to redirect it to:
http://www.mysite.com/pages.php/fruit
The original address does NOT exist any more. I have tried to construct a rewrite but this is not quite working how I want it to work
RewriteEngine on
RewriteCond %{QUERY_STRING} =category=fruit`
RewriteRule ^pages\.php$ pages.php/fruit/ [L,R=301]
goes to
http://www.mysite.com/home/linux123/m/mysite.com/user/htdocs/pages.php/fruit/
Any advice on fixing the construction of the rewrite rule would be great. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
URL 重写的工作方式是,它采用不存在的 URL 并将其重写为指向存在的 URL。不存在的 URL 更多的是表示性的东西,而不是功能性的东西。你正在做相反的事情,网页上的链接应该像
http://www.mysite.com/pages.php/fruit
,当用户点击它们时,他们应该在内部被转发到类似http://www.mysite.com/pages.php?category=fruit
的内容。必须相应地编写重写规则,如果类别严格按字母顺序排列,则为字母数字,
您甚至可以使用此在线验证器测试您的正则表达式重写规则;
正则表达式验证器
希望这有帮助..
The way URL rewriting works is that it takes non-existant URL and rewrites it to point to the one that exists. The non-existant URL is more of presentation thing rather than a functional thing. You are doing it the other way round, the links on your web pages should be like
http://www.mysite.com/pages.php/fruit
and when the user clicks on them they should internally be forwarded to something like thishttp://www.mysite.com/pages.php?category=fruit
. The rewrite rule has to be written accordingly which would beif the category is strictly alphabetical otherwise for alphanumeric
You can even test your regex rewrite rules using this online validator;
Regex Validator
Hope this helps..