问题:
是否可以清除%{QUERY_STRING}
,而不会破坏我在.htaccess文件中完成的任何功能。
我想防止用户将?foo=bar
添加到URL的末尾覆盖任何预定义的$ _GET vars
(外部re_write)示例:
来自:example.com/foo/bar/hello/world?test=blah
to: example.com/foo/bar/hello/world
到目前为止,我已经能够(内部)完成此操作:
来自:example.com/foo/bar/hello/world
to: example.com/index.php?foo=bar&hello=world
.htaccess
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GETs.+.(?:html?|php|aspx?) [NC]
RewriteRule ^(.+).php$ /$1 [R=301,L,NC]
# To externally redirect /dir/index to /dir/
RewriteCond %{THE_REQUEST} ^GETs((.+)?(/)?)?(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
我在某个地方阅读并经历了它,以应用上述内容破坏了我已经完成的任何事情。如果将整个URL掩盖为$ _get vars,则完全清除整个URL。甚至崩溃了Apache 0.O(请原谅我的任何愚蠢的正则表达式或.htaccess代码,它们并不是我的坚固套房,大部分是我在SO上找到的片段)
)这可能吗?我做错了什么?
谢谢
尝试:
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}
变量将中有一些内容,您的规则将使它们陷入困境。如果您与实际请求匹配,则您的重写不会受到影响。