将网页从HTTP重定向到HTTPS

时间:2014-05-02 19:14:19

标签: .htaccess http redirect https

我需要将网站中的3个页面从http重定向到https。我需要所有其他页面保持http。

我已经在这个网站和网上搜索了两天寻找有效的解决方案。没有提供完整的解决方案。

需要https的两个页面位于根目录中,一个位于子文件夹中。我一直在摆弄两个.htaccess文件,但我希望一切都可以在根文件中完成。

我是正则表达式和.htaccess指令的新手,所以如果解决方案对某些人来说很明显,我会道歉。

这是迄今为止我已整理的根.htaccess文件。我对您对代码的任何部分的反馈持开放态度。谢谢你的帮助!

RewriteEngine On

# redirects non-www page requests to www version
RewriteCond %{HTTP_HOST} ^mywebsite.com
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]

# PERTINENT CODE BLOCK BELOW

# page (order-form) has to be on https **[SEEMS TO WORK]**
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^/order-form/$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

# page (employment-application) has to be on https  **[SEEMS TO WORK]**
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^/employment-application/$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

# page (about/contact-us) has to be on https **[NOT WORKING]**
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^/about/contact-us/$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

# All other pages have to be on http **[NOT WORKING]**
RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !^/order-form/$ [NC]
RewriteCond %{REQUEST_URI} !^/employment-application/$ [NC]
RewriteCond %{REQUEST_URI} !^/about/contact-us/$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]

# END - PERTINENT CODE BLOCK

# remove .php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# disables directory view on web pages
Options -Indexes

# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"

# send 404 errors to home page
# info at http://httpd.apache.org/docs/current/mod/core.html#errordocument
ErrorDocument 404 /

1 个答案:

答案 0 :(得分:0)

将此.htaccess保留在DocumentRoot

# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"

# send 404 errors to home page
# info at http://httpd.apache.org/docs/current/mod/core.html#errordocument
ErrorDocument 404 /

Options -Indexes
RewriteEngine On

# redirects non-www page requests to www version
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L,NE]

# adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# 3 pages to be on https
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} ^/(order-form|employment-application|about/contact-us)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]

# All other pages have to be on http
RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !^/(order-form|employment-application|about/contact-us)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301,NE]

# remove .php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php [L]