简体   繁体   中英

multiple htaccess redirects with http to https but except page

i use mod_rewrite to allow "friendly URLS" to replace search parameters, which works fine. However i need to now redirect all traffic to https:// but allow only some pages to remain on http:// (i need to do this so my referrals are collected by the sites i send traffic to)

Over the years i have added to the .htaccess file, most works OK but i dont fully understand it, so it may be getting messy too :-(

i have the below (i have cut out anything i dont think is relevant)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]
</IfModule>

<IfModule mod_rewrite.c>

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

The above works OK, but redirect ALL pages to https

I have tried adding /changing the https redirect part to the below to allow urlout.php not to be redirected, but this just loops and i get a browser warning that the page has too many redirects :-(

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]

I have tried a few other things too, but they either dont redirect and everything goes to https, or i get a loop error or internal 500 error...

Basically i want all pages to go to https, but not urlout.php

Any advice on any of the code above would me much appreciated!

Answering my own questions:

i managed to do this, was a silly mistake of me not adding [NC] after the first condition...

However, for others, the below code will redirect ALL to https:// from http:// with the exception of 1 page (urlout.php in my case)

# redirects any http:// traffic to https:// with exception of urlout.php
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/urlout\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

I needed to do this, so that sites i send traffic to can see my domain as the "HTTP referrer".

I however found a simpler way to pass the referrer from https to http (although browser specific!) of adding to the HTML head:

<meta name="referrer" content="origin">

My ref for this was (among others): moz.com's Meta Referrer Tag Blog Post

If you want to redirect some spacific pages to https, forexample, To redirect :

to

And

to - https://www.example.com/page2.html

You may try the following :

 RewriteEngine on

 RewriteCond %{HTTPS} off
 RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]

If you want to redirect the whole site from http to https, you can use the following :

 RewriteEngine on



 RewriteCond %{HTTPS} off
 RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]

RewriteCond %{HTTPS} off is important to avoid redirect loop as it skips the rule for https requests.

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