Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

When I´m in a secure part of a web-site I´m developing (entering a specific page or folder using https), the relative links to the normal pages are automatically converted to https as well.

Is there a way to tell an <a> tag to always use the http protocol instead of the https protocol (or the other way around)?

I know it´s easy using the complete link (http://www.mysite.com/index.html or https://www.mysite.com/index.html), but I would like it to work with relative links (index.html, ../index.html, etc.).

like image 674
jeroen Avatar asked Feb 25 '09 00:02

jeroen


People also ask

How do I create a relative link?

Complete HTML/CSS Course 2022 To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.

What is HTML href?

What is the HTML a href attribute? In HTML, the inline a (anchor) element denotes a hyperlink from one web address to another. All functional a elements must contain the href (hypertext reference) attribute inside the opening a tag. The href attribute indicates the destination of the hyperlink.

Do you need https in href?

So yes you need to put http, https, or www, just to let browser know that you redirecting user somewhere outside of this site(server), or it will try to find path you specified on current server. It is because each link is just a path to some document and protocol explicitly says that it is other server.


2 Answers

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.

like image 168
Matthew Avatar answered Sep 21 '22 19:09

Matthew


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

like image 39
Jans Carton Avatar answered Sep 18 '22 19:09

Jans Carton