Short way to link to http from https (and vice versa) using relative links

Not sure if this is exactly what you were looking for, but if you're using Apache, there's a trick you can use to do this: http://httpd.apache.org/docs/2.2/ssl/ssl_faq.html#relative

Basically you put the following in your Apache configuration file:

RewriteEngine on
RewriteRule ^/(.*):SSL$ https://%{SERVER_NAME}/$1 [R,L]
RewriteRule ^/(.*):NOSSL$ http://%{SERVER_NAME}/$1 [R,L]

then append :SSL to any link (in the webpage) for which you want to force HTTPS, and :NOSSL to any link for which you want to force regular HTTP.


We use Apache mod_rewrite to control whether a page is served via http or https. Here's an example snippet from a site's root directory .htaccess file:

# Redirect most reqests for https to http
RewriteRule ^https://www.example.com(.*) http://www.example.com$1 [R=301,NC]

# Allow some URIs to be https if requested
RewriteCond   %{SERVER_PORT}  ^443$
RewriteCond   %{REQUEST_URI}  !^/images/(.*)$
RewriteCond   %{REQUEST_URI}  !^/scripts/(.*)$
RewriteCond   %{REQUEST_URI}  !^/styles/(.*)$
RewriteCond   %{REQUEST_URI}  !^/store(.*)$
RewriteCond   %{REQUEST_URI}  !^/login.htm$
RewriteRule ^(.*) http://www.example.com/$1 [L,R]

# Force some URIs to be https only
RewriteCond   %{SERVER_PORT}  ^80$
RewriteRule ^store(.*) https://www.example.com/store$1 [L,R]

RewriteCond   %{SERVER_PORT}  ^80$
RewriteRule ^FormSanityKey.htm https://www.example.com/login$1 [L,R]

Read all about it at:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html


You can make all your internal links be protocol independent by using the following syntax:

<a href="//some/file/on/your/domain.php">link text</a>

instead of

<a href="some/file/on/your/domain.php">link text</a>

the // will make the link respect whatever protocol the page was loaded over.

Tags:

Html

Http

Https