【发布时间】:2012-09-06 05:14:07
【问题描述】:
我在.htaccess文件中写了很多RewriteRule,但是当我从https页面切换到http页面时出现问题;它不遵守这些规则
注意: 在 localhost 上一切正常,问题出在服务器上 UPDATE
这是我的website ,目前所有链接都按照RewriteRule*
例如关于我们页面链接显示为
http://www.mywebsite.com/about
但是
如果我在login page(在https)并点击关于我们页面然后它会变成下面。
http://www.mywebsite.com/about?slug=about_us
或者如果我点击左侧面板上的任何类别,那么它会出现
http://www.mywebsite.com/auction/category/1?cid=1
注意:即使鼠标悬停在页面上也会显示重写链接
下面是.htaccess 文件,其中包含所需信息。
IndexIgnore *
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^auction/category/([0-9]+)/?$ bids.php?cid=$1 [NC]
RewriteRule ^login/?$ login.php [NC]
RewriteRule ^register/?$ register.php [NC]
RewriteRule ^logout/?$ logout.php [NC]
# static pages
RewriteRule ^about/?$ page.php?slug=about_us [NC]
# Rewrite to https
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login [OR]
RewriteCond %{REQUEST_URI} /do_login.php
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L,QSA]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|ico|css|js)$ - [NC,L]
# traffic to http:// except some pages
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(/login|/do_login.php)
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L,QSA]
注意: Here 是完整的 .htaccess 文件
请告诉我我哪里错了/错过了什么?
我还有一些困惑
- 如果我更改重写 URL 的大小写(Login 或 lOgin 或 logiN),那么它会报错?
-
[NC,L] 与所有
RewriteRule一起写是一种好习惯吗? - 我应该在什么时候写[QSA]?
更新
根据所有答案的建议,更改RewriteRule 几乎解决了所有问题,但现在还有最后一个问题。
-
/loginURL 始终更改为/login.php。
下面是我更新的.htaccess
IndexIgnore *
Options -MultiViews
Options +FollowSymLinks
#mod_rewrite
RewriteEngine on
RewriteBase /
# Rewrite www to non www
RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# minimize the css on all http:// pages
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
#switch over http to https
# Rewrite to https
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (/login|/do_login)\.php [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|ico|css|js)$ - [NC,L]
# traffic to http:// except some pages
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(/login|/do_login)\.php [NC]
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/?$ login.php [NC]
RewriteRule ^register/?$ register.php [NC]
# ...many other rules...with [NC] falg
RewriteRule ^auction/category/([^/.]+)/?$ bids.php?cid=$1 [NC]
RewriteRule ^about/?$ page.php?slug=about_us [NC]
# ...many more rules.... with [NC] flag
【问题讨论】:
-
在您的登录页面中,如果我将鼠标悬停在它显示的关于链接 https://www.charityrummage.com/about,关于我们页面的链接不应该在 http 中吗??
-
是的鼠标悬停显示右链接,但如果你点击这个然后看到链接?
-
QSA 仅在您更改查询字符串(通过在替换中使用 ?)并且您想要附加旧字符串时才有意义。不要将 NC,L 附加到每个规则中。如果您希望它不区分大小写,请使用 NC。在 htaccess 中,使用 L 会导致当前轮次处理以当前替换结束——要么你的 htaccess 依赖于此,要么不依赖。
标签: php apache .htaccess mod-rewrite https