的.htaccess HTTPS重定向单页(使用相对URL)重定向、htaccess、HTTPS、URL

2023-09-02 09:53:52 作者:漃寞數煙圈:

我需要能够从标准的HTTP单个页面重定向到https。比如,我想去从 http://domain.com/quote.php 到的 https://domain.com/quote.php 。

到目前为止,我用这个code。在我的.htaccess文件,和它的工作初始重定向。

 的RewriteCond%{} SERVER_PORT!^ 443 $
重写规则^ quote.php https://domain.com/quote.php [R = 301,L]
 

我的问题是,一旦我访问了quote.php页面并重定向到https的版本,其他所有的网站页面浏览我继续使用https协议。这是一个潜在的重复内容的问题,因为我现在已经通过HTTP和HTTPS访问相同的内容。

所以,我想要做的,就是能够做到以上重定向,然后以某种方式做同样的事情相反的除外quote.php所有页面。所以,如果你试图通过HTTPS访问它们,它会重定向到默认的HTTP版本。

我使用相对URL整个网站,所以我不能简单地硬code在HTTPS / HTTP preFIX。我需要能够经由.htacess做到这一点,如果可能的话。

任何帮助是极大的AP preciated。

解决方案

 的RewriteCond%{} HTTPS关闭
重写规则^ quote.php $ https://domain.com/quote.php [R = 301,L,QSA]

的RewriteCond%{} HTTPS上
的RewriteCond%{REQUEST_URI}!^ / quote.php
重写规则^(。*)$ http://domain.com/$1 [R = 301,L,QSA]
 

答案评论: 添加新的页面conndtions只是把它们parenthesis.like:

 的RewriteCond%{} HTTPS关闭
重写规则^(报价|接触)的.php $ https://domain.com/$1.php [R = 301,L,QSA]
 
IIS配置Url重写实现http自动跳转https的重定向方法 100 解决 我欲成魔 CSDN博客 iis url重写 不写条件 所有跳转都被重定向

问题2: QSA 标记添加当前的查询字符串新的URL。它发生在默认情况下,除非情况下更改查询字符串。您现在可以安全地删除它们,但如果你已经添加了查询字符串,并希望有老一过,把那回来了。

编辑2: code以上有一个小的安全问题:(,其实这是多一点 - D。 当您使用 HTTPS 来传递HTML codeS和页面使用相对路径,所以这是很好。但是当你把这些codeS的的.htaccess 他们变成 HTTP 键,这就是问题所在:-)。把code以下,以SOVE问题:):

 的RewriteCond%{} HTTPS关闭
重写规则^(报价|接触)的.php $ https://domain.com/$1.php [R = 301,L,QSA]


的RewriteCond%{} HTTPS上
的RewriteCond%{REQUEST_URI} ^ /(报价|联系我们)!.PHP
的RewriteCond%{REQUEST_URI} ^ /(。*)(CSS | PNG | JS |?JPE G | GIF | BMP)!$
重写规则^(。*)$ http://domain.com/$1 [R = 301,L,QSA]
 

现在,所有的图片,脚本..您正在使用的安全网页,正在传输安全。

I need to be able to redirect a single page from standard http to https. For example, I want to go from http://domain.com/quote.php to https://domain.com/quote.php.

So far I'm using this code in my .htaccess file, and it's working for the initial redirect.

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^quote.php https://domain.com/quote.php [R=301,L]

My problem, is that once I have visited the quote.php page and get redirected to the https version, all the other site pages I navigate to continue using the https protocol. This is a potential duplicate content issue, as I now have the same content accessible via http and https.

So what I want to do, is be able to do the above redirect, and then somehow do the same thing in reverse for all pages except quote.php. So if you attempted to access them via https, it would redirect to the default http version.

I use relative URLs throughout the site, so I can't simply hard-code in the https/http prefix. I need to be able to do this via .htacess, if possible.

Any help is greatly appreciated.

解决方案

RewriteCond %{HTTPS}  off
RewriteRule  ^quote.php$  https://domain.com/quote.php  [R=301,L,QSA]

RewriteCond %{HTTPS}  on
RewriteCond %{REQUEST_URI}   !^/quote.php
RewriteRule  ^(.*)$  http://domain.com/$1  [R=301,L,QSA]

answer to comment: for adding new page to conndtions just put them parenthesis.like:

RewriteCond %{HTTPS}  off
RewriteRule  ^(quote|contact).php$  https://domain.com/$1.php  [R=301,L,QSA]

question 2: QSA flag add current query string to new URL. it happens by default except in case you change query string. You can delete them now safely but if you have added query string, and wanted to have old one too, put that back.

Edit 2: code above has a little security issue :(, actually it's more than a little :-D. when you are using https to transfer html codes and page is using relative paths, so that's fine. but when you put these codes in .htaccess they turn into http and that's the problem:-). put the code below to sove the problem:):

RewriteCond %{HTTPS}  off
RewriteRule  ^(quote|contact).php$  https://domain.com/$1.php  [R=301,L,QSA]


RewriteCond %{HTTPS}  on
RewriteCond %{REQUEST_URI}   !^/(quote|contact).php
RewriteCond %{REQUEST_URI}   !^/(.*).(css|png|js|jpe?g|gif|bmp)$
RewriteRule  ^(.*)$  http://domain.com/$1  [R=301,L,QSA]

Now, all images,scripts,.. that you are using on secure pages, are transferring securely.