How to specify a protocol in a binding and use a relative path?

On a personal website, it is always preferable to use relative paths in the bindings so that you can easily move the directory structure to the new website, but I want to make part of the password protected site (using .htaccess) and therefore use HTTPS. Is there a way to specify a different protocol in href binding without hard coding www.domain.com ?

Example:

 <a href="/student/example">Link</a> 

I want to turn it into something like:

 <a href="https: /student/example">Link</a> 

The above obviously does not work. Is there any way to do this? I try to avoid any scripting, but I will appreciate JavaScript more than PHP or ASP.

+7
source share
4 answers

I think there is no β€œsimple” solution ... And javascript seems like a bad idea for such purposes.

You can try:

 <base href="https://yourdomain.com/"> 

placed in the title of the document.

OR

Your idea:

 <a href="/test-me1/">regular link 1</a> <a href="/test-me2/">regular link 2</a> <a href="/https/test-me3/">secure link</a> 

and below, but before closing the body tag, put something like this:

 <script type="text/javascript"> (function(){ var h = 'yourdomain.com'; /* could be replaced with something like window.location.host */ var a = document.links; for (var c = 0; c < a.length; c++) { var i = a[c].href.indexOf('/https/'); if(-1 !== i) { a[c].href = 'https://' + h + '/' + a[c].href.substring( i + 7 ); /* where 7 is a length of '/https/' */ } } })(); </script> 

OR even just:

 <script type="text/javascript"> (function(){ var a = document.links; for (var c = 0; c < a.length; c++) { if(-1 !== a[c].href.indexOf('/https/') ) { a[c].href = a[c].href.replace('/https/','').replace('http:','https:'); } } })(); </script> 
+1
source

You cannot do this without relying on Javascript to dynamically change href. The way in which relative URIs are converted to absolute URIs is described in RFC 3986 Section 5.2.2 :

 if defined(R.scheme) then T.scheme = R.scheme; T.authority = R.authority; T.path = remove_dot_segments(R.path); T.query = R.query; else if defined(R.authority) then T.authority = R.authority; T.path = remove_dot_segments(R.path); T.query = R.query; else if (R.path == "") then T.path = Base.path; if defined(R.query) then T.query = R.query; else T.query = Base.query; endif; else if (R.path starts-with "/") then T.path = remove_dot_segments(R.path); else T.path = merge(Base.path, R.path); T.path = remove_dot_segments(T.path); endif; T.query = R.query; endif; T.authority = Base.authority; endif; T.scheme = Base.scheme; endif; T.fragment = R.fragment; 

Where R is the relative URL and T is the target.

The above basically says that if the scheme is specified in the relative URI, then the entire uri will be the target uri, so the only way to specify the scheme is to specify the entire URL.

If you want to use the Javascript approach, you can dynamically set href using something like: a.href = 'https://' + window.location.host + a.getAttribute('href') . Where a is your AnchorElement.

Here is a demo of the Javascript version: http://jsfiddle.net/7DWV5/

However, mainly because you encounter it, it is recommended that you store your hostname in the configuration file or discover it from the HTTP Request Host header in the front controller. This way you can simply insert the template into your HTML code when creating the page. This eliminates the need to use client scripts to fix your URLs after they are created, which may be desirable because not everyone has Javascript.

0
source

You must add https to the specific URLs of your site using .htaccess as well. Make sure your .htaccess file contains this line:

 RewriteEngine on 

Then try adding this line after it:

 RewriteRule "^/student(/.*)" "https://%{HTTP_HOST}$1" [R=301,L] 

http://www.askapache.com/htaccess/ssl-example-usage-in-htaccess.html#redirect-urls-to-ssl

0
source

In JavaScript, you can do something like:

 <script> function handleLink(a){ var path = a.getAttribute('href'), host = location.hostname; location.href = 'https://'+host+path; return false; } </script> <a href="/student/example" onclick="return handleLink(this)">Link</a> 

I would not do that. If you want the user to use SSL, I would have a redirect page to the SSL page first

0
source

All Articles