删除的.htaccess查询字符串,保持SEO风格的URL [英] .htaccess remove query string, keeping SEO style url

查看:291
本文介绍了删除的.htaccess查询字符串,保持SEO风格的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

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

Is it possible to clear the %{QUERY_STRING} without breaking any functionality of what I've accomplished in my .htaccess file.

我想prevent用户添加?富=酒吧到URL的末尾覆盖任何predefined $ _GET瓦尔

I want to prevent users from adding ?foo=bar to the end of the url overwriting any predefined $_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

我读某处,并经历过,那将上述休息什么我已经完成了。彻底清除整个URL,如果他们都被屏蔽$ _GET瓦尔。甚至崩溃的Apache 0.o(原谅我的任何愚蠢的正则表达式或的.htaccess code,他们是不是我的浴室,大部分是片断我发现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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆