在htaccess中放置https重定向的位置

时间:2017-08-23 07:48:48

标签: php apache .htaccess redirect

我刚刚将我的网站移至https,因此我想将重定向放入我的htaccess中,以便任何前往http://的人都会被强制使用https://。

但是,我的htaccess中已经有一个片段,这对我的网站来说是必要的(将所有其他网址重写为index.php)。当我删除它时,重定向工作,但我的网站不会指向其他页面。

这是我目前的htaccess -

Options -indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Allow any files or directories that exist to be displayed directly

# Rewrite all other URLs to index.php/URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]  
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType application/pdf "access plus 1 year"
ExpiresByType audio/x-wav "access plus 1 year"
ExpiresByType audio/mpeg "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/quicktime "access plus 1 month"
ExpiresByType video/x-ms-wmv "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access 1 year"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>

<Files 403.shtml>
order allow,deny
allow from all
</Files>

在不弄乱网址的情况下,我需要包含哪些内容以及强制用户使用https而不是http?

2 个答案:

答案 0 :(得分:1)

试试这个:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L] 

答案 1 :(得分:1)

As soon as you can您希望他们尽快切换到安全区域,因为这是最安全的选择。切换到https将重定向它们(〜重新加载),因此htaccess无论如何都会运行两次。也可以使第一个尽可能小的速度运行 此外,切换到htaccess是为了安全,其余的只是漂亮。安全&gt;漂亮。

在您的情况下,在“# Rewrite all other URLs to index.php/URL”之前

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]

# Rewrite all other URLs to index.php/URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]