3

I'm creating a site that will be served over https when deployed. That means urls in the links of the site et cetera are https://... But now it's in an emerging state and I preview it on localhost.

Basically I just want to be able to keep the urls beginning with https and still be able to preview the site on localhost. The whole site will be https when deployed, so during development I don't need to know which is which, I just need to be able to preview the url even though they say https.

I could create a self-signed certificate for the page on localhost ... but I wonder if there is an easier, maybe ugly, way?

I run Apache on Linux.


A workaround would of course be to replace https with http during development and then switch back when the site is deployed. In my case this would only need to be done in one place (where I set the constant defining the hostname).

  • If you're testing with a web browser, just add a security exception for the site and store it in the browser. You usually do this when the browser says it's blocking the site due to security issues. – JMC Mar 28 '16 at 18:50
  • @JMC Could you elaborate a bit on that? I don't think the server (I use Apache for localhost) will serve https: urls if I don't have a certificate set up? I just get a "Connection failed" and I thought that was the standard behaviour? – PetaspeedBeaver Mar 28 '16 at 18:56
  • 1
    What is your goal here? Do you want to test the general functionality of the website on localhost, without getting errors because of the https links (in which case you could probably work with htaccess rewrite rules or some browser plugins to automatically go to http instead of https), or do you want to also test the correctness of the links (eg it's https if it needs to be) and everything else that may have to do with https (this would be more difficult without actually setting up https)? And what server setup do you have (apache, php, ...)? – tim Mar 28 '16 at 19:00
  • 1
    @tim "without getting errors" - you wouldn't be able to use "htaccess rewrite rules" to avoid an "error", since any HTTPS/SSL security error would occur before .htaccess gets a chance to do anything. – MrWhite Mar 28 '16 at 19:17
  • 1
    @w3dk You are right, I didn't really think that through, there would of course still be errors. That would probably leave some kind of browser plugin, like a reverse https everywhere. – tim Mar 28 '16 at 19:39
2

I use the solution of not using absolute links within the site to itself. Instead of:

<a href="https://example.com/page.html">

Use protocol relative links:

<a href="//example.com/page.html">

Or root relative links:

<a href="/page.html">

Then your site works on your development server even if you view it over http rather than https.

  • Nice idea. Are there any potential drawbacks with this approach? – PetaspeedBeaver Mar 28 '16 at 20:27
  • 1
    I do this with all my sites. It saves bytes as well. The only drawback is that if somebody (or a crawler) finds your site on an alternate domain, they won't make their way back to the canonical site name by clicking links. – Stephen Ostermiller Mar 28 '16 at 20:30
  • Classic, simple, and perfect. – closetnoc Mar 28 '16 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.