.htaccess 删除查询字符串,保留 SEO 风格的 url [英] .htaccess remove query string, keeping SEO style url
问题描述
问题:
是否可以在不破坏我在 .htaccess 文件中完成的任何功能的情况下清除 %{QUERY_STRING}
.
Is it possible to clear the %{QUERY_STRING}
without breaking any functionality of what I've accomplished in my .htaccess file.
我想阻止用户在 url 末尾添加 ?foo=bar
覆盖任何预定义的 $_GET 变量
I want to prevent users from adding ?foo=bar
to the end of the url overwriting any predefined $_GET vars
(外部重写)示例:
来自: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 变量,则完全清除整个 url.甚至崩溃 apache 0.o(请原谅我任何愚蠢的正则表达式或 .htaccess 代码,它们不是我的强大套件,其中大部分是我在 SO 上找到的片段)
I read somewhere, and experienced it, that applying the above breaks anything I've already accomplished. Completely clearing the whole url if they are masked $_GET vars. Even crashes apache 0.o (forgive me for any silly regex or .htaccess code, they're aren't my strong suite and most of it was snippets I found on SO)
这可能吗?我做错了什么?
is this possible? something I'm doing wrong?
谢谢
推荐答案
尝试:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string
您需要根据实际请求进行匹配,因为您正在使用以下规则构建查询字符串:
You need to match against the actual request because you're building a query string with these rules:
# 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}
变量将包含内容,而您的规则将破坏它们.如果您与实际请求匹配,则您的重写不会受到影响.
When these rules loop around, the %{QUERY_STRING}
variable will have stuff in it and your rules will clobber them. If you match against the actual request, your rewrites won't get affected.
这篇关于.htaccess 删除查询字符串,保留 SEO 风格的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!