使用 mod_rewrite 使用不同的 get 变量进行重定向

发布于 2024-09-12 17:17:02 字数 204 浏览 0 评论 0原文

使用 mod_rewrite,我将如何获取如下网址:

www.example.com/?foo

=hello www.example.com/?foo=world

并将它们重定向到如下网址:

www.example.com/hello/

www.example .com/world/

谢谢

Using mod_rewrite, how would I take urls like these:

www.example.com/?foo=hello

www.example.com/?foo=world

and redirect them to urls like these:

www.example.com/hello/

www.example.com/world/

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半岛未凉 2024-09-19 17:17:03

你应该做的事情恰恰相反。您使网站上的所有 URL 链接看起来像“漂亮”版本(但不更改任何其他功能),然后使用 mod_rewrite 制作将匹配“漂亮”URL 请求并显示相应“丑陋”的模式网址。一个非常简单的链接示例...

RewriteEngine On
RewriteEngine ^([^/]+)/?$ /index.php?foo=$1

更新:

您可以使用以下命令向相反方向重定向...

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=(.*)
RewriteRule ^index.php$ /%1/ [R=302]

一旦您对它的工作感到满意,您可以将 302 更改为 301 以实现永久重定向。如果将此与上面的示例结合起来,您将收到 500 内部服务器错误,因为您将创建重定向循环。

What you should be doing is the opposite of that. You make all URL links on your site look like the 'pretty' version (but don't change any other functionality), then you use mod_rewrite to make patterns that will match requests for a 'pretty' URL and show the appropriate 'ugly' url. A very simple example for your link...

RewriteEngine On
RewriteEngine ^([^/]+)/?$ /index.php?foo=$1

Update:

You can redirect in the opposite direction using...

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=(.*)
RewriteRule ^index.php$ /%1/ [R=302]

Once you are happy it's working, you can change the 302 to 301 for a permanent redirect. If you combine this with the above example though you will get a 500 Internal Server Error since you will be creating a redirect loop.

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