.htaccess - 除了索引之外的一切目标(使用干净的URL)

时间:2015-03-26 16:43:07

标签: regex apache .htaccess mod-rewrite ssl

我的代码是:

<IfModule mod_rewrite.c>

RewriteEngine On

# Detect Protocol
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]

# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force WWW and correct protocol
#RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^\.htaccess$ - [F]
RewriteRule ^php\.ini$ - [F]

RewriteRule ^cgi-bin - [R=404]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png|js|css|swf)$
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

我想在主页上强制使用SSL除外。因此,我需要查看请求的地址是否等于&#34; /&#34;

我无法弄明白。我尝试了以下所有,但我似乎需要一个htaccess大师:

RewriteCond $1 !^(/)
RewriteCond %{REQUEST_URI} !(/)
RewriteCond %{REQUEST_URI} !(/$)

......还有其他一些人。很确定我需要在

之后添加它
RewriteCond %{HTTPS} !=on

但是我被困了

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine On

# Detect Protocol
RewriteCond %{HTTPS} on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} off
RewriteRule ^ - [env=proto:http]

# Force SSL except for home page
RewriteCond %{HTTPS} off
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force WWW and correct protocol
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^\.htaccess$ - [F]
RewriteRule ^php\.ini$ - [F]
RewriteRule ^cgi-bin - [R=404]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|js|css|swf)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]