.htaccess删除查询字符串,保留SEO风格的url

.htaccess删除查询字符串,保留SEO风格的url,.htaccess,mod-rewrite,apache2,.htaccess,Mod Rewrite,Apache2,问题: #if there is a query string RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule (.*) $1? [R=301,L] #remove query string 是否可以清除%{QUERY\u STRING},而不破坏我在.htaccess文件中完成的任何功能 我想防止用户在url末尾添加?foo=bar,覆盖任何预定义的$\u GET vars (外部重新写入)示例: 发件人:example.com/foo/bar

问题:

#if there is a query string
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteRule (.*) $1? [R=301,L] #remove query string
是否可以清除
%{QUERY\u STRING}
,而不破坏我在.htaccess文件中完成的任何功能

我想防止用户在url末尾添加
?foo=bar
,覆盖任何预定义的$\u GET vars

(外部重新写入)示例:

发件人:
example.com/foo/bar/hello/world?test=blah

致:
example.com/foo/bar/hello/world

到目前为止,我已经(在内部)完成了这项工作:

#if there is a query string
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteRule (.*) $1? [R=301,L] #remove query string
发件人:
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
-

-

我在某个地方读到,并且体验到,应用上面的方法会破坏我已经完成的任何事情。如果它们被屏蔽,则完全清除整个url$\u GET vars。甚至会导致Apache0.o崩溃(请原谅我的任何愚蠢的regex或.htaccess代码,它们不是我的强大套件,大部分都是我在上面找到的代码片段)

这可能吗?我做错什么了

谢谢

试试:

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\u STRING}
变量将包含内容,您的规则将对其进行重击。如果您与实际请求匹配,您的重写不会受到影响。

与其他人得到403时的错误相同,看起来它删除了查询字符串。但是url被重写为
example.com/C:/PATH/to/SITE/DIRECTORY/www/foo/bar
(在使用xampp测试的windows box上,目标计算机是*nix box,不确定这是否会影响它)@strokeforcezero噢,因此,您需要一个
RewriteBase/
,或者在规则目标中的
$1
前面添加一个
/
。谢谢您,好先生,您是个天才!=)(我将浏览你的android应用程序,因为我正在浏览你的网站,等待回复哈哈)