mod_rewrite:从 URL 中删除查询字符串? [英] mod_rewrite: remove query string from URL?

查看:23
本文介绍了mod_rewrite:从 URL 中删除查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

*?page=1 重定向到 *

*?page=1 redirects to *

(其中 * 是通配符).

(where * is a wildcard).

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

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.

有什么快速的方法吗?

推荐答案

应该这样做:

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

逐行:

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

如果您希望重定向是临时的 (302),那么您可以删除 =permanent 部分.临时移动是 R 标志的默认值.

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.

这篇关于mod_rewrite:从 URL 中删除查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆