Apache HTTP RewriteRule for hostname - apache

everyone.
My problem is simple, but I cannot manage to find a solution.
All I need to do is use RewriteRule on Apache to redirect all request from a hostname to its FQDN.
For example, if the request is https://hostname/test.html, I want Apache to redirect the request to https://hostname.test.com/test.html. This has to be rewritten to any request to that hostname.
How can I do this?
Edit 1:
I've noticed that the following RewriteRule works for HTTP, but not for HTTPS:
RewriteCond %{​​​​​​​HTTP_HOST}​​​​​​​ ^hostname$
RewriteRule ^(.*)$ https://hostname.test.com%{​​​​​​​​REQUEST_URI}​​​​​​​​ [R,L]
What am I missing?
BR

I've managed to find the problem.
The redirect is working.
The problem is that the certificate does not contain the hostname version of the URL, only the FQDN.
Since the certificate is checked before the redirect, hence the problem.
Sorry for the mess up

Related

Where to handle the redirection from HTTP to HTTPs?

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.

301 Redirect in .htaccess gives certificate warning on original URL first

My site has a certificate but it's not a wildcard certificate. So it's for example.com, not for *.example.com.
Not a problem I thought, I'll just redirect any visitor to the proper URL through mod_rewrite:
RewriteEngine On
RewriteBase /
# Following two lines to strip machine name
RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [L,R=301]
# Following two lines make sure the https version is always served
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301]
Now, the redirect actually works. When someone types in https://www.example.com/page, he will eventually be redirected to https://example.com/page.
But...
The browser first displays a warning that https://www.example.com is insecure. Only when I add an exception, will it be redirected to https://example.com/page which does not give a certificate error...
What am I doing wrong here?
Nothing. SSL negotiation occurs at the transport (TCP) level, not HTTP (even when using SNI) but the point is that the certificate is not valid for the requested domain. When the connection is initiated to www. the browser will request the certificate and compare the url with the CN in the cert and since it isn't there, it'll raise the alert.
To resolve this issue you will need a certificate that includes both ServerName and ServerAlias names. You could maybe try some DNS provider that offers DNS HTTP redirection, but getting a certificate is quite easy this days.

Web server keeps redirecting to HTTP

I'm trying to use CloudFlare page rule to redirect to https but when I set it up it gives me the redirect loop and keeps changing back and fourth between http and https. I deleted my .htaccess file and deleted every redirect I had in the control panel. Still not working. Neither my webhost support or CloudFlare support can help. This is my page rule setup as CloudFlare support told me to set it:
URL pattern: http://.kohlercoding.dk/
setting: always use https
If you can edit the .htaccess file try this:
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^www.website.com$
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]
I just found out the problem is that I had my SSL setting on "Full" on CloudFlare. That means I have to have a verified certificate on my webhost which I didn't. That's why it didn't work. So changing it to "Flexible" fixed my problem. Or having a self-signed certificate on the webhost if they support it and, unlike UnoEuro, doesn't make you pay for HTTPS Protection.

Redirect from https to http url stored from google

in a search from google , i found my domain url with https and not http .
For example : https://xxxx.com/yyyy/zzzz and not http://xxxx.com/yyyy/zzzz
It's possible redirect from https to http for the domain xxxx.com ?
I use centos and apache web server
On the same server, i have a certificate https that respond to https://zzzz.com
Thanks
Carlo
This will work with mod_rewrite on. Put this code in .htaccess file at root of the site.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

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]