2012-10-20 66 views
1

问:的.htaccess删除查询字符串,保持SEO风格的URL

是否有可能清除%{QUERY_STRING}没有打破我在我的.htaccess文件已经完成的任何功能。

我想防止用户添加?foo=bar到URL重写任何预定义的$ _GET结束瓦尔

(外部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变量。甚至崩溃apache 0.o(原谅我的任何愚蠢的正则表达式或.htaccess代码,他们不是我的强大的套件,大部分是我发现在SO上的片段)

这是可能的吗?我做错了什么?

感谢

回答

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}变量将会有东西在里面,你的规则会打破它们。如果您与实际请求相匹配,您的重写将不会受到影响。

+0

同样的错误我与其他人得到403,它看起来像它删除查询字符串。但url被重写为'example.com/C:/ PATH/TO/SITE/DIRECTORY/www/foo/bar'(在用xampp测试的Windows盒子上,目标机器是* nix盒子,不确定这是否会影响它) – StrikeForceZero

+1

@StrikeForceZero哦,所以你需要一个'RewriteBase /'或者在规则目标中的$ 1前面加一个'/'。 –

+0

谢谢你好先生,你是个天才!=)(我会偷看你的android应用程序,因为我正在浏览你的网站等待响应哈哈) – StrikeForceZero

相关问题