将网页从HTTP重定向到HTTPS [英] Redirect web pages from HTTP to HTTPS

查看:273
本文介绍了将网页从HTTP重定向到HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to re-direct 3 pages in a website from http to https. I need all other pages to remain http.

我一直在搜索这个网站和网络两天寻找有效的解决方案。没有提供完整的解决方案。

I've been scouring this site and the web for two days looking for a solution that works. Nothing has provided a complete solution.

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

Two pages that need https are in the root directory and one is in a subfolder. I've been fiddling with both .htaccess files, but I'm hoping everything can be done in the root file.

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

I'm new to regular expressions and .htaccess directives, so I apologize if the solution is obvious to some of you.

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

Here's the root .htaccess file I've put together so far. I'm open to your feedback on any part of the code. Thanks for your help!

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 /


推荐答案

保持这个.htaccess in DocumentRoot

Keep this .htaccess in 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]

这篇关于将网页从HTTP重定向到HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆