[ACCEPTED]-Short way to link to http from https (and vice versa) using relative links-https

Accepted answer
Score: 30

You can make all your internal links be 4 protocol independent by using the following 3 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 2 respect whatever protocol the page was loaded 1 over.

Score: 10

We use Apache mod_rewrite to control whether 3 a page is served via http or https. Here's 2 an example snippet from a site's root directory 1 .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:


Score: 4

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

Basically 5 you put the following in your Apache configuration 4 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 3 which you want to force HTTPS, and :NOSSL to any 2 link for which you want to force regular 1 HTTP.

Score: 3

It sounds like you want to use the BASE element.

There is 8 no way to 'tell' an Anchor to use a specific 7 protocol or not, at least without using 6 the BASE element in the HEAD of that page.

Is 5 there a reason why you want to use relative 4 links over absolute links? This article talks about 3 the pitfall of using relative links with 2 HTTPS - the potential to have your site 1 double indexed and such.

Score: 2

There is no way to do this with relative 3 hyperlinks as your are using a different 2 protocol, it would be no different than 1 linking to a ftp location.

Score: 0

I use this code to create a link of the 3 current index page to direct to the https server, because 2 we provide different content according the 1 protocol and we do not know where is hosting.

  <a>Secure server
      let url = new URL(window.location);
      url.protocol = 'https:';
      url.port = '443';
      document.currentScript.parentElement.href = url.toString();

More Related questions