I have the following in my htaccess file:
我的htaccess文件中有以下内容:
RewriteEngine On
RewriteBase /
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$
# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Send to index.php for clean URLs
RewriteRule ^(.*)$ index.php?/$1 [L]
This does work. It hides index.php, and it adds a trailing slash... except when there is a query string.
这工作。它藏起了索引。加上一个斜杠…除非有查询字符串。
This URL:
这个网址:
http://example.com/some-page
gets redirected to:
被重定向到:
http://example.com/some-page/
but this URL:
但这个URL:
http://example.com/some-page?some-var=foo&some-other-var=bar
does not get redirected. I would like for the above to be sent to:
不重定向。本人欲将上述文件寄交:
http://example.com/some-page/?some-var=foo&some-other-var=bar
I've reached the limits of my understanding of redirects with this. If you have a working answer, I would really appreciate a walkthrough of what every line is doing and why it works. Double bonus awesomeness for an explanation of why what I have right now doesn't work when there is a query string involved.
我已经达到了我对重定向的理解的极限。如果你有一个有效的答案,我将非常感激你对每一行都在做什么以及为什么它会这样做。当涉及到查询字符串时,为什么我现在所拥有的东西不能工作?
2 个解决方案
#1
2
Try adding a [QSA]
to the end of the last Redirect rule to preserve the original query string as below
尝试在最后一个重定向规则的末尾添加一个[QSA],以保持原始查询字符串如下所示
# Send to index.php for clean URLs, preserve original query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
a walkthrough of what every line is doing and why it works.
每一行都在做什么,为什么会起作用。
See my comments below
看到我的评论
#turn mod_rewrite engine on.
RewriteEngine On
#set the base for urls here to /
RewriteBase /
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### and the URI does not end with a /
RewriteCond %{REQUEST_URI} !(.*)/$
### redirect and add the slash.
RewriteRule ^(.*)$ $1/ [L,R=301]
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to index.php passing the URI as a path, QSA will preserve the existing query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
#2
1
I believe that if you change this:
我相信如果你改变这一点:
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
to this:
:
RewriteCond %{REQUEST_URI} !^([^?]*)/($|\?)
RewriteRule ^([^?]*) $1/ [L,R=301]
then it should do what you want.
那么它应该做你想做的。
The changes I made are:
我所做的改变是:
- In both rewrite-condition and -rule, I changed
(.*)
and^(.*)
to^([^?]*)
, to ensure that, if there's a query-string, then it is not included in either regex. ([^…]
means "any character that is not in…
", so[^?]
means "any character that is not a question mark".) - 重写条件和规则,我改变了(. *)、^(. *)^(^ ? *),以确保,如果有一个查询字符串,它不包括在正则表达式。([^…]意味着“任何字符不在…”,所以[^ ?意思是“任何不是问号的人物”。
- In the rewrite-condition, I changed
$
to($|\?)
, so as to match either end-of-URL or end-of-part-before-the-query-string. - 在rewrite条件中,我将$更改为($|\?),以便匹配url端点或查询字符串前的部分端点。
- In the rewrite-rule, I dropped the
$
, since it was no longer needed. - 在rewrite规则中,我删除了$,因为它不再需要了。
#1
2
Try adding a [QSA]
to the end of the last Redirect rule to preserve the original query string as below
尝试在最后一个重定向规则的末尾添加一个[QSA],以保持原始查询字符串如下所示
# Send to index.php for clean URLs, preserve original query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
a walkthrough of what every line is doing and why it works.
每一行都在做什么,为什么会起作用。
See my comments below
看到我的评论
#turn mod_rewrite engine on.
RewriteEngine On
#set the base for urls here to /
RewriteBase /
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### and the URI does not end with a /
RewriteCond %{REQUEST_URI} !(.*)/$
### redirect and add the slash.
RewriteRule ^(.*)$ $1/ [L,R=301]
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to index.php passing the URI as a path, QSA will preserve the existing query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
#2
1
I believe that if you change this:
我相信如果你改变这一点:
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
to this:
:
RewriteCond %{REQUEST_URI} !^([^?]*)/($|\?)
RewriteRule ^([^?]*) $1/ [L,R=301]
then it should do what you want.
那么它应该做你想做的。
The changes I made are:
我所做的改变是:
- In both rewrite-condition and -rule, I changed
(.*)
and^(.*)
to^([^?]*)
, to ensure that, if there's a query-string, then it is not included in either regex. ([^…]
means "any character that is not in…
", so[^?]
means "any character that is not a question mark".) - 重写条件和规则,我改变了(. *)、^(. *)^(^ ? *),以确保,如果有一个查询字符串,它不包括在正则表达式。([^…]意味着“任何字符不在…”,所以[^ ?意思是“任何不是问号的人物”。
- In the rewrite-condition, I changed
$
to($|\?)
, so as to match either end-of-URL or end-of-part-before-the-query-string. - 在rewrite条件中,我将$更改为($|\?),以便匹配url端点或查询字符串前的部分端点。
- In the rewrite-rule, I dropped the
$
, since it was no longer needed. - 在rewrite规则中,我删除了$,因为它不再需要了。