Apache使用www +将所有URL重写为https,但有一些例外情况

时间:2011-06-21 22:25:53

标签: .htaccess ssl redirect

我已经尝试过类似堆栈问题的所有答案,但没有任何效果。除了example.com/blogs/*和example.com/page-name之外,我需要将所有内容重定向到https://www

我目前有这个:

RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 

重定向除https://example.com以外的所有内容,它不会添加www。

您可以在https://moblized.com

找到自己

RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L] 
RewriteCond %{http_host} ^moblized.com [NC]
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L] 

RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} blogs 
RewriteRule ^(.*)$ http://moblized.com/blogs/$1 [R,L]

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

# $Id: .htaccess,v 1.90.2.4 2009/12/07 12:00:40 goba Exp $

AddHandler php5-script .php

谢谢!

1 个答案:

答案 0 :(得分:10)

我希望我能正确理解你。你想要:

  • example.com重定向到www.example.com(/ blogs /和/ page-name除外)
  • 将所有网页重定向到HTTPS(/ blogs /和/ page-name除外)
  • 根据您/page-name /favicon.ico下的当前.htaccess # activate rewrite engine RewriteEngine On # don't touch favicon.ico (always accept as is regardless of the domain or protocol) RewriteRule ^favicon.ico$ - [L] # don't touch /index.php (usually means already overwritten URL) # otherwise we may enter into a loop RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^index\.php$ - [L] # ensure trailing slash is present for /blogs -> /blogs/ RewriteRule ^blogs$ http://mobilized.com/blogs/ [R=301,QSA,L] # /blogs/ should only be accessible via http://example.com/blogs/ RewriteCond %{HTTP_HOST} !^moblized\.com$ [NC] RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L] RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC] RewriteCond %{HTTPS} =on RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L] RewriteRule ^blogs/.* - [L] # redirect to www.example.com if necessary RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC] RewriteCond %{REQUEST_URI} !=/client-ipad-contest RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L] # redirect to HTTPS if not there already RewriteCond %{HTTPS} !=on RewriteCond %{REQUEST_URI} !=/client-ipad-contest RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L] # Rewrite URLs of the form 'x' to the form 'index.php?q=x'. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]

以上是上述要求的规则 - 将它们放入.htaccess:

https://example.com
BTW,如果您的客户转到example.com,浏览器很可能会显示“不受信任的证书”警告。这是因为在Apache的重写模块开始处理请求之前,必须首先完全建立HTTPS会话。

如果这是问题 - 那么考虑购买另一个SSL证书(或来自其他供应商),该证书将涵盖www.example.com*.example.com(GoDaddy肯定会这样做)或获取将涵盖的通配证书所有子域名 - {{1}}(但这很可能会更加昂贵)。


更新:在本地模拟您的需求之后(抱歉,我没有运行Apache的SSL,所以我用不同的规则/域名替换了它(在我的测试中))我修改了并更新了规则。

我已经在本地测试了这些规则(所有页面都非常简单,只包括1张图片和css以及一些文字) - 一切看起来都不错。如果有什么不起作用,请告诉我。