.htaccess 重定向不符合我的正则表达式
.htaccess redirects aren't respecting my regex
我需要将来自一组来源 URL 的带有查询字符串的任何请求重定向回感谢页面。
例如我需要重定向:
http://example.com/test1/test2/[origin]/?id=1
回到
http://example.com/thank-you
我在 .htaccess 文件中设置它的方式是这样的:
RewriteEngine On
RedirectMatch 302 ^/test1/test2/(.*)/.+ /thank-you
我已经测试了我在在线正则表达式测试器中使用的正则表达式,它似乎按预期工作,所以我很困惑为什么没有进行重定向。 Here's the link to that.
显然,我必须添加反斜杠来转义正则表达式测试器中 URL 中的斜杠,但根据我对 .htaccess 如何计算正则表达式的理解,这些不是必需的。
我的问题是:如果我从正则表达式字符串的末尾删除 .+
,则重定向在没有查询字符串的页面上完美运行,这意味着正则表达式的开头部分工作正常。我不明白为什么查询字符串与我创建的正则表达式不匹配。
我也试过:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L]
对于您的 RedirectMatch,您可以使用:
RedirectMatch 302 ^/test1/test2/(.*)/(.*)+ /thank-you?
对于您的 RewriteRule 部分,您可以使用:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L,QSD]
首先,不需要 RewriteEngine On
和 mod_alias
,按照你的规则 RedirectMatch
将它与 mod_rewrite
一起使用,第二个规则。
试试这个:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^test1/test2/[^\/]+/$ /thank-you? [R=302,L]
我使用 ^id=([0-9]+)$
来限制以 id
开头并以 numerical value
结尾的查询字符串。
我删除了这一行 RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
因为你也可以匹配 RewriteRule
中的 URI。
如果此规则无效,请将 [R=302,L]
更改为 [R=301,L]
以永久重定向。
注意:清除浏览器缓存再测试
我需要将来自一组来源 URL 的带有查询字符串的任何请求重定向回感谢页面。
例如我需要重定向:
http://example.com/test1/test2/[origin]/?id=1
回到
http://example.com/thank-you
我在 .htaccess 文件中设置它的方式是这样的:
RewriteEngine On
RedirectMatch 302 ^/test1/test2/(.*)/.+ /thank-you
我已经测试了我在在线正则表达式测试器中使用的正则表达式,它似乎按预期工作,所以我很困惑为什么没有进行重定向。 Here's the link to that.
显然,我必须添加反斜杠来转义正则表达式测试器中 URL 中的斜杠,但根据我对 .htaccess 如何计算正则表达式的理解,这些不是必需的。
我的问题是:如果我从正则表达式字符串的末尾删除 .+
,则重定向在没有查询字符串的页面上完美运行,这意味着正则表达式的开头部分工作正常。我不明白为什么查询字符串与我创建的正则表达式不匹配。
我也试过:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L]
对于您的 RedirectMatch,您可以使用:
RedirectMatch 302 ^/test1/test2/(.*)/(.*)+ /thank-you?
对于您的 RewriteRule 部分,您可以使用:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L,QSD]
首先,不需要 RewriteEngine On
和 mod_alias
,按照你的规则 RedirectMatch
将它与 mod_rewrite
一起使用,第二个规则。
试试这个:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^test1/test2/[^\/]+/$ /thank-you? [R=302,L]
我使用 ^id=([0-9]+)$
来限制以 id
开头并以 numerical value
结尾的查询字符串。
我删除了这一行 RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
因为你也可以匹配 RewriteRule
中的 URI。
如果此规则无效,请将 [R=302,L]
更改为 [R=301,L]
以永久重定向。
注意:清除浏览器缓存再测试