Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanent redirect from http to https page

Tags:

.htaccess

When I try to create rules for redirect from HTTP pages to HTTPS pages (only for specific pages) with .htaccess, I've received loop redirect. Where am I wrong?

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/doctor) [NC, OR]
RewriteCond %{REQUEST_URI} ^(/client)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(/doctor) [NC, OR]
RewriteCond %{REQUEST_URI} !^(/client)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
like image 450
misho Avatar asked Feb 08 '11 12:02

misho


People also ask

How do I permanently redirect a website?

A 301 is used when a page has permanently changed location, and a 302 should be used if you intend to move the page back under the original URL in the future. In general, you should expect to use 301 redirects on your website.


4 Answers

to force https for a particular folder use

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

for entire site use

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

for specific pages you could use

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/doctor/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]

Check if server port is different from 443 (standard for secure connections), to ensure we are going to redirect only non-secured connections

Redirect the page secure.php to secure domain, sending 301 response status, appending all query string and marking is as last rule, so any rules below this will not be parsed (since this is redirect, we don't want to take any other actions)

here is a solution without using .htaccess i found here

    <?php

//assuming you site structure like www.domain.com/p=doctor

    $redirectlist = array('doctor','nurse','anyother');

    if (in_array($_GET['p'], $redirectlist) && strtolower($_SERVER['HTTPS']) != 'on') {
        exit(header("location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"));
    }

?>
like image 78
ayush Avatar answered Nov 09 '22 04:11

ayush


I've found answer to my question:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/(client/|doctor/))
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(/(client/|doctor/))
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
like image 35
misho Avatar answered Nov 09 '22 04:11

misho


Try this:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^/doctor/?.*$
RewriteCond %{REQUEST_URI} ^/client/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If https is not on, and you are accessing /doctor or /client (or any sub-folders of those two), it should forward to HTTPS.

like image 23
Alistair Evans Avatar answered Nov 09 '22 04:11

Alistair Evans


Similar to Kazar's answer, but more concise.

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule !/(doctor|client)(/.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
like image 20
Alex Grin Avatar answered Nov 09 '22 03:11

Alex Grin