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

jet*_*lej 2 .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] 
Run Code Online (Sandbox Code Playgroud)

它重定向除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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Laz*_*One 10

我希望我能正确理解你.你要:

  • 重定向example.comwww.example.com(除了/ blogs /和/ page-name)
  • 将所有页面重定向到HTTPS(/ blogs /和/ page-name除外)
  • 基于你当前的.htaccess /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]
Run Code Online (Sandbox Code Playgroud)

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

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


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

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