.htaccess强制www和强制https为某些路径和Concrete5漂亮的URL

时间:2014-10-06 10:54:46

标签: .htaccess mod-rewrite redirect https concrete5

编辑:此问题现在没有用,因为整个网站将使用HTTPS提供

我的.htaccess导致重定向循环。

我需要所有三个部分都可以工作,每个部分的目的都在评论中。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force www prefix unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_PROTOCOL}://www.%{HTTP_HOST}/$1 [L,R=301]

# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

我不得不在实时服务器上工作,因为证书只在那里有效(需要www.)。

我已经尝试了上述的许多变化,但我很难过,所以我希望新鲜的眼睛能有所帮助,非常感谢。

1 个答案:

答案 0 :(得分:0)

您的第二条规则看起来不正确,因为REQUEST_PROTOCOL的值为HTTP/1.1

您可以使用此代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# Force www prefix unless dev or already there
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} !^(?:www|dev)\. [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>