简体   繁体   中英

How to rewrite from http requests to https on specific pages of my site?

Hey people. I see that this topic is repeated over and over on SO but I tried several solutions posted here and none of them worked quite for me. So basically - I know how to change specific pages on my website so that they're in https mode. However, I dunno how to rewrite the http requests that are INSIDE the code. So for example, if my page, say, payment.php, contains link that loads external ccs file like this -> http://example.com/somecss.css then google chrome will see it as dangerous link and display cross-red padlock next to site url. Now, I've tested it manually by changing all the http requests inside the code to https and the padlock magically became green so I guess I need some kind of mod rewrite rule that would rewrite all the links on those specific pages so that they would contain https. I hope I explained it well enough. Can anyone help me? I'm using codeigniter.

To rewrite the http requests inside the code you have to use protocol-relative paths there.

<link rel="stylesheet" href="//www.domain.com/style.css"> 
<script src="//www.domain.com/script.js"></script> 

It will automatically use the protocol of the parent page

You have to make sure that the user is browsing your site over secure connection. You can redirect the user to secure connection (https://) using an .htaccess file containing the following lines:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Please, note that the .htaccess should be located in the web site main folder.

In case you wish to force HTTPS for a particular folder you can use:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]

The .htaccess file should be placed in the folder where you need to force HTTPS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM