mod_rewrite:从 URL 中删除查询字符串?

发布于 2024-09-13 19:53:45 字数 222 浏览 4 评论 0原文

我正在尝试使用 .htaccess 进行以下重定向(301)

*?page=1 重定向到 *

(其中 * 是通配符)。

基本上,我只是想阻止任何人访问 URL 末尾带有 ?page=1 的页面,而是将他们定向到相同的 url 减去 ?page=1

有没有快速的方法来做到这一点?

I'm trying to make the following redirection (301) using .htaccess

*?page=1 redirects to *

(where * is a wildcard).

Basically, I just want to prevent anyone accessing a page with ?page=1 at the end of the URL, and instead direct them to the same url minus ?page=1.

Is there a quick way of doing this?

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

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

发布评论

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

评论(4

唱一曲作罢 2024-09-20 19:53:45

这应该可以做到:

RewriteEngine    On
RewriteCond      %{QUERY_STRING}    ^page=1$
RewriteRule      (.*)               $1?     [R=permanent]

逐行:

  1. 您打开重写功能。
  2. 您指定一个条件(“if 语句”),查询字符串必须完全是 page=1 才能应用以下规则。
  3. 然后指定一条规则,表示用自身 ($1) 替换整个路径 (.*),但将查询字符串设为空 (?),并使结果成为永久重定向 (301)。

如果您希望重定向是临时的 (302),那么您只需删除 =permanent 部分即可。临时移动是 R 标志的默认设置。

This should do it:

RewriteEngine    On
RewriteCond      %{QUERY_STRING}    ^page=1$
RewriteRule      (.*)               $1?     [R=permanent]

Line by line:

  1. You turn on the rewriting functionality.
  2. You specify as a condition ("if statement") that the query string has to be exactly page=1 for the following rules to apply.
  3. Then you specify a rule that says substitute the entire path (.*) with itself ($1), but make the query string empty (?), and make the result a permanent redirect (301).

If you want the redirect to be temporary (302) then you can just remove the =permanent part. Moved Temporarily is the default for the R flag.

哽咽笑 2024-09-20 19:53:45

您还可以使用QSD标志(查询字符串丢弃)来重定向某处而不传递查询字符串。

You can also use the QSD flag (Query String Discard) to redirect somewhere without passing the query string.

累赘 2024-09-20 19:53:45

如果您使用的是 Apache 2.4,则只需使用 QSD(查询字符串丢弃标志)即可丢弃目标 URL 中的特定查询字符串。

以下是 Apache 2.4 用户的示例:

旧 URL : - /foo/bar/?page=1

新 URL : - /foo/bar/

.htaccess 代码:

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI} [L,R,QSD]

上面的规则将使用 ?page=1 重定向任何 URI,以删除查询字符串。此示例将在低于 2.4 的 Apache 版本上返回 500 错误,因为它们不支持 QSD

在较低版本的 Apache 上,您可以在目标 URL 末尾使用空问号 ? 来删除查询字符串。

示例:

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

上面的示例几乎适用于所有版本的 Apache。

If you are on Apache 2.4 You can simply use the QSD (Query String Discard flag) to discard the specific query strings from your destination URL.

Here is an example for Apache 2.4 users:

Old URL : - /foo/bar/?page=1

new URL : - /foo/bar/

.htaccess code:

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI} [L,R,QSD]

The Rule above will redirect any URI with ?page=1 to remove the query strings. This example will return 500 error on Apache versions bellow 2.4 as They don't support QSD.

On lower versions of Apache, you may use an empty question mark ? at the end of the destination URL to remove query strings.

An example:

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

The example above works almost on all versions of Apache.

最近可好 2024-09-20 19:53:45

对于剥离整个查询字符串,这就足够了:

RewriteRule ^(.*) http://example.com/$1? [R=301,L]

For stripping entire query string this will be enough:

RewriteRule ^(.*) http://example.com/$1? [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文