如何让浏览器在重写时显示干净的 URL?

发布于 2024-09-07 13:32:59 字数 572 浏览 0 评论 0原文

我有一个看起来像这样的超链接:

http://domain.com/sample/comments/65

当我单击它时,它会变成这样:

http://domain.com/sample/comments/index.php?submissionid=65

我正在使用重写规则来使其执行此操作。这就是我想要的,除了我还希望浏览器中显示的 URL 仍然看起来像“http:// /domain.com/sample/comments/65。”

我该怎么做? .htaccess 文件显示如下。

RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ http://domain.com/sample/comments/index.php?submissionid=$1 [NC,L]

预先感谢,

约翰

I have a hyperlink that looks like this:

http://domain.com/sample/comments/65

And when I click on it, it goes to this:

http://domain.com/sample/comments/index.php?submissionid=65

I'm using a rewrite rule to make it do this. This is what I want, except I also want the URL displayed in the browser to still look like "http://domain.com/sample/comments/65."

How can I do this? The .htaccess file is displayed below.

RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ http://domain.com/sample/comments/index.php?submissionid=$1 [NC,L]

Thanks in advance,

John

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

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

发布评论

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

评论(1

浅笑轻吟梦一曲 2024-09-14 13:32:59

您必须删除 http://domain.com/sample/ 部分,否则它将强制重定向:

RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ comments/index.php?submissionid=$1 [NC,L,B]

B 标志也是必需的,因为您正在使用反向引用在查询字符串内,需要转义。

手册说(强调我的):

在 .htaccess 文件中使用重写引擎时,每个目录的前缀(对于特定目录始终相同)会自动删除以进行模式匹配,并在替换完成后自动添加。此功能对于多种重写都是必不可少的;如果没有这个,您将始终必须匹配父目录,但这并不总是可能的。 有一个例外:如果替换字符串以 http:// 开头,则不会添加目录前缀,并且会强制执行外部重定向(或代理吞吐量,如果使用标志 P)。有关详细信息,请参阅 RewriteBase 指令。

如果您将重写规则放在虚拟主机或主配置中,只要请求主机与重写规则中的主机匹配,就不会出现这种情况。

You must remove the part http://domain.com/sample/, otherwise it will force a redirect:

RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ comments/index.php?submissionid=$1 [NC,L,B]

The B flag is also necessary because you're using the backreference inside a query string, which requires escaping.

The manual says (emphasis mine):

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting; without this, you would always have to match the parent directory, which is not always possible. There is one exception: If a substitution string starts with http://, then the directory prefix will not be added, and an external redirect (or proxy throughput, if using flag P) is forced. See the RewriteBase directive for more information.

This would not be case if you put the rewrite rule in the virtual host or main configuration as long the request host and the host in the rewrite rule matched.

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