htaccess 重写/重定向

发布于 2024-12-12 12:45:00 字数 532 浏览 0 评论 0原文

我在构建重写规则时遇到一些问题。我想要重写并最终重定向的网址包含一个搜索查询,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-12-19 12:45:00

URL 重写的工作方式是,它采用不存在的 URL 并将其重写为指向存在的 URL。不存在的 URL 更多的是表示性的东西,而不是功能性的东西。你正在做相反的事情,网页上的链接应该像 http://www.mysite.com/pages.php/fruit ,当用户点击它们时,他们应该在内部被转发到类似 http://www.mysite.com/pages.php?category=fruit 的内容。必须相应地编写重写规则,

^pages\.php/([A-Za-z])*$ pages.php?category=$1 [NC,L]

如果类别严格按字母顺序排列,则为字母数字,

^pages\.php/([A-Za-z0-9])*$ pages.php?category=$1 [NC,L]

您甚至可以使用此在线验证器测试您的正则表达式重写规则;

正则表达式验证器

希望这有帮助..

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 this http://www.mysite.com/pages.php?category=fruit. The rewrite rule has to be written accordingly which would be

^pages\.php/([A-Za-z])*$ pages.php?category=$1 [NC,L]

if the category is strictly alphabetical otherwise for alphanumeric

^pages\.php/([A-Za-z0-9])*$ pages.php?category=$1 [NC,L]

You can even test your regex rewrite rules using this online validator;

Regex Validator

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文