将网页从HTTP重定向到HTTPS

编程入门 行业动态 更新时间:2024-10-22 12:34:40
本文介绍了将网页从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 RewriteRule (.*) www.mywebsite/$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 ^(.*)$ www.mywebsite/$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 ^(.*)$ www.mywebsite/$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 ^(.*)$ www.mywebsite/$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 ^(.*)$ www.mywebsite/$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 httpd.apache/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 httpd.apache/docs/current/mod/core.html#errordocument ErrorDocument 404 / Options -Indexes RewriteEngine On # redirects non-www page requests to www version RewriteCond %{HTTP_HOST} ^mywebsite\$ [NC] RewriteRule (.*) www.mywebsite/$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 ^(.*)$ %{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_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

本文发布于:2023-10-12 16:57:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   网页   HTTP   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!