在htaccess中强制https

时间:2012-07-18 22:08:44

标签: apache .htaccess mod-rewrite https

我的网站获得了证书,现在我需要将其移至https。这是我的htaccess:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

我该怎么办?感谢

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到;这是一个:

## port requirement (bail if not 443)
RewriteCond %{SERVER_PORT} !^443$
## exceptions
RewriteCond %{REQUEST_URI} ^/somepath$ [OR]
RewriteCond %{REQUEST_URI} ^/anotherpath$
## force traffic to https equivalent
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我添加了2个可选的RewriteCond条目,允许您根据URI的第一个组件指定例外。