问题:
是否可以清除%{QUERY_STRING}
而不破坏我在.htaccess文件中完成的任何功能。
我想阻止用户在网址末尾添加?foo=bar
覆盖任何预定义的$ _GET vars
(外部re_write)示例:
来自:example.com/foo/bar/hello/world?test=blah
收件人:example.com/foo/bar/hello/world
到目前为止,我已经能够(内部)完成此任务:
来自:example.com/foo/bar/hello/world
收件人:example.com/index.php?foo=bar&hello=world
的.htaccess
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.(?:html?|php|aspx?) [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To externally redirect /dir/index to /dir/
RewriteCond %{THE_REQUEST} ^GET\s((.+)?(\/)?)?(index) [NC]
RewriteRule ^(([^/]+/)*)index$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
# Prevent Rewrite on existing directories, files, and links
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L] #^ - [L]
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
我尝试了什么:
#if there is a query string
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ $1? [R=301,L] #remove query string
我在某个地方读过,并且经历过它,应用上述内容打破了我已经完成的任何事情。完全清除整个网址,如果它们被屏蔽$ _GET vars。甚至崩溃apache 0.o(请原谅我任何愚蠢的正则表达式或.htaccess代码,它们不是我强大的套件,而且大部分是我在SO上发现的片段)
这可能吗?我做错了什么?感谢
答案 0 :(得分:3)
尝试:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string
您需要与实际请求进行匹配,因为您正在使用以下规则构建查询字符串:
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
当这些规则循环时,%{QUERY_STRING}
变量将包含其中的内容,您的规则将破坏它们。如果您与实际请求匹配,则您的重写不会受到影响。