重定向规则以从URL删除查询字符串
一直试图用查询字符串编写重定向规则但没有成功。
我有 URL example.com/blog/?page=1
或 example.com/blog/?hello
,因此查询字符串中的内容并不重要。如何编写重定向规则,以便它剪切查询字符串并重定向到查询字符串之前的 URL。例如,这两个 URL 都必须重定向到 example.com/blog/
,以便 URL 不包含任何查询字符串。
我正在尝试 RewriteRule ^blog/?$ blog/ [R=301,L,NE]
但被重定向到 404 页面。
也尝试过 RewriteRule ^blog/?$ /blog/ [R=301,L,NE]
并收到页面无法正常工作的消息,“URL”重定向您太多次。
顺便说一句,我使用的技术是带有 htaccess 插件的 Gatsby。
Have been trying to write a redirect rule with query string but did not succeed.
I have the URL example.com/blog/?page=1
or example.com/blog/?hello
, so it does not really matter what goes in the query string. How do I write a Redirect rule, so that it cuts the query string and redirects to the URL before the query string. For example, both of those URLs have to redirect to example.com/blog/
so that URL does not contain any query string.
I was tryingRewriteRule ^blog/?$ blog/ [R=301,L,NE]
but got redirected to 404 page.
Also triedRewriteRule ^blog/?$ /blog/ [R=301,L,NE]
and got the message that page is not working, 'URL' redirected you too many times.
BTW, technology I am using is Gatsby with htaccess plugin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要删除查询字符串,您首先需要检查是否有一个查询字符串要删除,否则,它无能为力。
例如,要从
/blog/?<查询启用>
删除查询字符串,您将执行类似的操作:这与URL PATH
blog/code>匹配斜线可选),并将重定向到
/blog/
(带有落后斜线)。您的示例URL包括拖延的斜线,但是您的正则表达式似乎暗示了尾斜线是可选的吗?前面的条件(
rewriteCond
指令)检查query_string
服务器变量以确保这是非空的(即它匹配单个字符,用点表示)。替换中的
$ 1
backReference 字符串中包含从前一个重新写入
staters 的捕获组中的值。 IE。在此示例中“博客”。这只是节省了重复。您可以很容易地编写重写 ^blog/?$/blog/[qsd,r,l]
。QSD
(查询字符串丢弃)标志从重定向的响应中删除原始查询字符串,否则,默认情况下将通过(将创建一个redirect-loop)。如果请求不包含查询字符串,则此规则无能为力(因为条件将失败)。
如果要永久性,则将302(临时)重定向更改为301(永久),但只有在您按预期确认这项工作后。浏览器将301持续缓存,因此可以使测试有问题。
看您现有规则:
默认情况下,相对替代字符串(即
blog/code>)被视为相对于包含
.htaccess
文件的目录,而此”然后将目录prefix“然后再将返回到相对URL,因此(默认情况下)将导致形式的畸形重定向https://example.com/path/path/path/path/pable/public_html/blog/
。这不是检查(或删除)查询字符串,因此基本上只是将其重定向到本身 - 无尽的重定向环。
删除任何任何 url的任何查询字符串
修改
重写
模式匹配任何URL并将其重定向到相同。例如:这需要在 root
.htaccess
文件的顶部进行。如果
.htaccess
文件在子目录中(不是根),则您需要做以下操作,因为$ 1
backReference(如上所述) 't包含完整的根层url路径。To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.
For example, to remove the query string from
/blog/?<query-string>
you would do something like this:This matches the URL-path
blog/
(trailing slash optional) and redirects to/blog/
(with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?The preceding condition (
RewriteCond
directive) checks theQUERY_STRING
server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).The
$1
backreference in the substitution string contains the value from the captured group in the precedingRewriteRule
pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily writeRewriteRule ^blog/?$ /blog/ [QSD,R,L]
instead.The
QSD
(Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).If the request does not contain a query string then this rule does nothing (since the condition will fail).
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.
A look at your existing rules:
By default, the relative substitution string (ie.
blog/
) is seen as relative to the directory that contains the.htaccess
file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the formhttps://example.com/path/to/public_html/blog/
.This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.
Remove any query string from any URL
Modify the
RewriteRule
pattern to match any URL and redirect to the same. For example:This needs to go at the top of the root
.htaccess
file before any existing rewrites.If the
.htaccess
file is in a subdirectory (not the root) then you will need to do something like the following instead, since the$1
backreference (as used above) won't contain the complete root-relative URL-path.