2012-11-11 51 views
0

我已经搜索了一些在这个网站上,但无法找到正确的答案给我的问题。奇怪的htaccess查询参数

我想强制一个www重定向并强制每个url的双向间隙。

我在我的htaccess下面几行:

# enable rewriting 
RewriteEngine on 

# if not a file or folder, use index.php 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [L] 

# force www 
RewriteCond %{HTTP_HOST} !^www 
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# force endslash 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301] 

我的WWW重定向工作正常,它就会像:

http://example.com 

to 

http://www.example.com 

但现在奇怪的部分是,我结束斜杠线正在添加我不想要的url参数。

所以这就会像:

http://www.example.com/path/without/endlash 

to 

http://www.example.com/index.php/?url=path/without/endslash 

为什么我定义的URL参数在这种情况下使用,以及如何防止这种情况。

在此先感谢


编辑:

感谢icrew

只有不加档/无目录的endlash条目之前,因为否则会重定向我的资产。

最终代码:

RewriteCond %{HTTP_HOST} !^www 
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !=/index.php 
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
+0

好吧,如果请求的URL不能解析为一个物理文件或目录那么你的第一个重写规则匹配和不重写你观察。这是你已经编程作为你的第一条规则... – arkascha

回答

1

的.htaccess

RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [L] 

的这部分负责http://www.example.com/index.php/?url=path/without/endslash重定向。所以基本上,如果你不想在查询字符串中的url参数删除这三行。

编辑:我弄清楚你想要什么。贝娄是正确的代码

# enable rewriting 
RewriteEngine on 

# force www 
RewriteCond %{HTTP_HOST} !^www 
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# force endslash 
RewriteCond %{REQUEST_URI} !(.*)/$ 
#next row prevent redirection if final rewriting is done 
RewriteCond %{REQUEST_URI} !=/index.php 
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301] 

# if not a file or folder, use index.php 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
#QSA because I suppose you dont want to discard the existing query string. Remove QSA if you want to discard 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
+0

但是我的脚本是这样制作的,所以一切都在我的index.php中处理。 所以我实际上需要这些线。 我不能使用这两个部分没有这个问题? – NLZ

+0

你想让finall url看起来像什么? http://www.example.com/index.php/path/without/endslash? – Igor

+0

谢谢你,你的更新代码就像一个魅力。 – NLZ