mod_rewrite:如何删除查询字符串的 url 但保留其值

发布于 2024-09-29 18:40:21 字数 431 浏览 1 评论 0原文

我想使用 mod_rewrite 删除其查询字符串的 URL,但保留查询字符串的值,例如, id 想要将:更改

http://new.app/index.php?lorem=1&ipsum=2

为漂亮的干净:

http://new.app/

但保留 lorem 和 < code>ipsum,所以在 index.php 中:

$_GET["lorem"]

仍会返回 1 等。

这是我第一次涉足 mod_rewrite,因此非常感谢任何帮助,如果你可以准确解释你的解决方案是如何工作的,我下次也可以学习一些!

谢谢!

I'd like to strip a URL of it's query string using mod_rewrite but retain the values of the querystring, for example, id like to change:

http://new.app/index.php?lorem=1&ipsum=2

to a nice clean:

http://new.app/

but retain the values of lorem and ipsum, so inside index.php:

$_GET["lorem"]

would still return 1 etc.

This is my first dabble with mod_rewrite so any help is greatly appreciated, and if you could explain exactly how your solution works, I can learn a little for next time too!

Thanks!

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

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

发布评论

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

评论(2

无人问我粥可暖 2024-10-06 18:40:21

正如罗兰提到的,你似乎不理解重写的工作方式。这通常是使用 .htaccess 中的 Apache mod_rewrite 来完成的,它会默默地将漂亮的 URL 重写为 php 脚本,如 /index.php?lorem=1&ipsum=2

甚至 Joomla 也使用 .htaccess,只不过它有一个单个重写规则,将所有内容传递给 PHP 脚本,该脚本在 PHP 中进行实际重写。

您不明白的是,“漂亮”版本中仍然需要存在一些东西,以便 php 脚本提取 $_GET["lorem"] 的值,

所以它就像 http://new.app/ lorem/http://new.app/section/lorem 然后(使用 .htaccess 中的 mod_rewrite)重写 PHP 脚本。

As Roland mentioned, you don't seem to understand the way rewriting works. It's typically done using Apache mod_rewrite in .htaccess, which silently rewrites the pretty URLs to the php script as /index.php?lorem=1&ipsum=2

Even Joomla uses .htaccess, except it has a single rewrite rule that passes EVERYTHING to a PHP script which does the actual rewriting in PHP.

What you are not understanding is that something still needs to exist in the "pretty" version for the php script to pull the value of $_GET["lorem"]

So it would be like http://new.app/lorem/ or http://new.app/section/lorem which would then (using mod_rewrite in .htaccess) rewrite TO the php script.

不再让梦枯萎 2024-10-06 18:40:21

我不明白你到底想要什么。您的第一个 URL 是外部表单,用户可以看到该 URL 并可以在浏览器中输入该 URL。

第二种形式几乎删除了所有信息,因此当您将其发送到服务器时,服务器应该如何知道lorem=1&ipsum=2

如果你的问题确实是

即使用户在网站深处单击包含大量信息的 URL,如何使浏览器中的 URL 看起来更漂亮?

那么有两种解决方案:

  1. 您可以将信息以小块的形式传递到服务器并将它们全部保存在会话中。我不喜欢这样,因为这样用户就无法获取 URL,将其显示给朋友并让他看到相同的页面。
  2. 您可以将整个网站置于仅包含一个 的 HTML 中。这样,顶级窗口的 URL 不会改变,只有内部 URL(浏览器不显示)会改变。

I don't understand exactly what you want. Your first URL is the external form, which the users see and can type into their browsers.

The second form has almost all information stripped, so when you send that to a server, how is the server supposed to know that lorem=1&ipsum=2?

If your question is really

How do I make the URLs in the browser look nice, even if the user is somewhere deep in the website clicking on URLs that carry lots of information?

then there are two solutions:

  1. You can pass the information in small bits to the server and save them all in a session. I don't like that because then the user cannot take the URL, show it to a friend and have him see the same page.
  2. You can have your entire web site in an HTML <frameset> containing only one <frame>. That way, the URL of the top-level window will not change, only the inner URL (which is not displayed by the browser) will.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文