【问题标题】:block QUERY_STRING in htaccess other then secified在 htaccess 中阻止 QUERY STRING 不是指定的
【发布时间】:2014-01-23 00:45:50
【问题描述】:

我当前的 htaccess 如下所示:

RewriteCond %{QUERY_STRING} !^ts=.*$ [L]
RewriteRule   ^$ index.php? [L]

RewriteRule   ^index\.html$ index.php [NC,L]
RewriteRule   ^(set_language)-([a-zA-Z_0-9]+)\.html$  index.php?language=$2&%{QUERY_STRING} [NC,L]
RewriteRule   ^([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)\.html$  index.php?g1=$2&%{QUERY_STRING} [NC,L]
RewriteRule   ^([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)\.html$  index.php?g1=$2&province=$3&%{QUERY_STRING} [NC,L]
RewriteRule   ^([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)\.html$  index.php?g1=$2&g2=$4&%{QUERY_STRING} [NC,L]
RewriteRule   ^([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)-([a-zA-Z_0-9]+)\.html$  index.php?g1=$2&g2=$4&g3=$6&%{QUERY_STRING} [NC,L]

前两行用于阻止 QUERY_STRING。主要是在我的页面链接是这样的

[TEXT]-[NUMBER].html
[TEXT]-[NUMBER]-[TEXT]-[NUMBER].html

等等。联系表单中的一个链接包含 GET 值

?ts=[NUMBER]

我真的不知道如何阻止所有 QUERY_STRING,除了最后一个链接中的那个。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    how to block all QUERY_STRING's except the one from last link

    使用这条规则:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} .+
    RewriteCond %{QUERY_STRING} !^ts=[^&]*$ [NC]
    RewriteRule ^ - [F]
    

    【讨论】:

    • 几乎可以工作,但现在它只有在查询中有“ts”时才允许页面。正如我提到的只有一页有它。所以基本上有两种情况:没有QUERY的链接(在“?”之后)和QUERY中只有一个带有“ts”的链接。这是 "!^(ts=[^&]*)?$" 我要找的东西吗?
    • 仍然不如预期,例如:calling "/" - works finecalling "/?ts=123" - works finecalling "/some_text-12.html" - throws errorcalling "/some_text-12.html?ts=123" - throws error 上面不应该抛出错误,应该抛出错误例如:"/some_text-12.html?some_random=value" 并且这些是我想摆脱的。
    • 你确定"/some_text-12.html?ts=123" 会抛出错误吗?可以试试新的浏览器测试吗?
    • 是的,我确定,我已经在几个浏览器上检查过了。您的最后一个代码似乎按预期工作:) 非常感谢您的帮助。您能否解释一下或指出我在哪里可以找到有关“ENV:REDIRECT_STATUS”的信息。到目前为止,我没有找到任何好的信息来源:/
    • ENV:REDIRECT_STATUSmod_rewrite 设置,一旦发生内部重写。我添加了该条件以避免在您的其他规则之后应用此规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多