Where to handle the redirection from HTTP to HTTPs? - express

I have a heroku application with a domain from godaddy.
My site is built with node and express.
My website is www.juanitacalendar.de and I want it to always redirect to HTTPS (no matter if the users types the www or not).
Should I handle this within Heroku? Within node/express? In my index.html?
I've read in another answer that I'm suppose to use this code that has to do with apache. I am clueless on where to put this piece of code though.
RewriteEngine On
RewriteCond %{HTTPS} !^on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

There are many ways you could achieve this.
In your situation, it looks like you can get Node/Express.js to redirect for you. See this answer for more information: Heroku NodeJS http to https ssl forced redirect.

Related

301 Redirect from http to https same page name

checked the Forum but could not find an ideal answer. I have recently installed a SSL Certificate on my site and in the process of creating 301 redirects via the .htaccess file for nearly 400 page urls (to keep Google happy). I thought of using;
redirect 301 /contact.php https://www.mydomainname.co.uk/contact.php
but it breaks the site. The only solution I have seen is;
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^contact\.php$ https://www.mydomainname.co.uk/contact.php [L,R=301]
The above seems a lot of code to use for each of the 400 pages! is there a quicker way with less code I can use in the .htaccess file?
Many thanks. Hope someone can advise.
There are two basic ways of redirecting pages with Apache: Redirect (of mod_alias) and RewriteRule etc. (of mod_rewrite).
Redirect is very simple: it will just redirect a single URL to another. It can be useful sometimes, but it's usefulness is limited to its simplicity: in the case of HTTP-to-HTTPS redirection, it can't differentiate between HTTP and HTTPS connections, so it will just try to redirect to HTTPS even if you're already on HTTPS (and thus you end up in an infinite redirect loop).
RewriteRule, on the other hand, is more advanced and flexible. You can use RewriteCond to conditionally redirect requests; in your case, you'd want to redirect requests only if they're on a HTTP connection.
As you mentioned, you want to redirect to HTTPS for many (I presume all) requests; you can easily do this with only a single rule:
# Enable rewrites
RewriteEngine on
# Only run next RewriteRule on HTTP connections (not HTTPS)
RewriteCond ${HTTPS} off
# Redirect any page to the same URL with https:// schema
RewriteRule (.*) https://${SERVER_NAME}/$1 [L,R=301]
(The ${SERVER_NAME} variable will automatically be equal to your domain name, so you can even use this on web servers with multiple domain names.)

.htaccess forward 'www.w.' to 'www.'

I have a weird issue where Google is indexing several of my site's pages as 'www.w.example.com' which is causing issues with my security certificate.
While I'm looking for a solution, I'd like to redirect this using .htaccess but I can't seem to get it to work.
Currently I have:
RewriteRule ^(.*)www\.w\.(.*)$/$ https://www.$1/$2 [R=301,L]
But it doesn't seem to work..
I have multiple domains for this site so ideally it needs to redirect to the correct domain e.g. https://www.example1.com or https://www.example2.com
It is a bad SEO practice to have the same content available on more than 1 URLs.
You need to decide on the best URL you would like to use and then do a 301 redirect of the others to it.
You need to use a RewriteCond for matching host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.w\.(.+) [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
However you might still get SSL cert warning because cert negotiation happens before mod_rewrite rules are invoked.

Showing HTTPS content in HTTP site, using RewriteRule

I have two domains, DomainA and DomainB - they are hosted separately.
DomainB is secure with SSL, while DomainA is not. I want that if a user enters http://DomainA.com/donation he will see the content of the secured https://DomainB.com without any redirects. I figured that maybe Redirect rule will do the trick (I searched both Google and Stackoverflow, with no luck)
I wrote the following RewriteRule in DomainA .htaccess
RewriteCond %{HTTP_HOST} ^DomainA.com/donation
RewriteRule ^(.*) https://DomainB.com/$1 [P]
My question is - The SSL of DomainB will still work? Let's say a user will enter DomainA.com/donation, what will he see? the secured content of DomainB with the certificate as usual? If not, it means that the only way to keep DomainB certificate is to redirect the whole page of DomainA using [R=301] ?
I can't check it live due to the request of my client.
SSL is also provided to prevent precisely this sort of thing. So I hope this is not possible.
You can use that in your http://DomainA.com root .htaccess:
RewriteEngine on
RewriteRule ^donation/? https://DomainB.com/ [NC,R=301]

https to http redirect through htaccess for specific page only

Though it may looks like a very common question, but nothing is worked for me. Below is my problem.
I need to redirect my domain from http to https through htaccess. (I found the code and it is worked fine for me). But at the same, i do not want to redirect to https for some video pages on my site (http://www.ptchoices.com/welcome/video/467f9fd9-d649-4910-923e-83eeccd13875). because of previously written redirect rule, it tends to endless redirect loop.
Please suggest me on the same.
Well, you can check to see if you're already on https before you redirect. If https is not on, it won't redirect. I believe this is what you're requesting. If not, i'll modify my answer.
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

How do I redirect some requests to another domain?

I set up a site and changed the old site's name. So temporarily 2 sites are running.
www.mydomain.com (new site)
legacy.mydomain.com (old site)
The problem is that still some traffic comes to the new site mistakenly and I don't want my rails application to handle that (rails is kinda slow and takes lotta resources).
Instead, I want apache to redirect it to the old one before it reaches to the rails app.
The wrong traffic url is always like "www.mydomain.com/bbs/...". So I want to filter out requests with '/bbs/' and redirect it to "legacy.mydomain.com/bbs/...". I think I can do that on my .htaccess file. But I don't know how to write the rule. mod_rewrite is installed on my server.
Can you help?
Thanks.
Sam
Try:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^bbs/ http://legacy.mydomain.com%{REQUEST_URI} [R=301,L]