Apache redirect from https - linux

I installed an SSL Certificate on my server, and https works fine.
Http redirect works fine for example http://login.example.com -> https://example1.com/index.html
But redirect from https not working, for example https://login.example.com -> https://example1.com/index.html.
its just go to the main page of my server https://redirect.example.com
Any suggestions on how to get the redirect from https to work?
Thanks !
<VirtualHost *:80>
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName redirect.mydomain.com
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Thanks!

I see that you have a duplicate ServerName directive in your SSL host declaration.
That could be causing trouble.
From the Apache documentation:
If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host.

Use server alias for your Rewrite Rule:
ServerAlias login.example.com
you can use multiple ServerAlias for multiple Rewrite Rule

What exactly do you mean by multiple rules ? Multiple domain names/subdomains ?
A plain redirect of http(s)://login.example.com to https://example1.com/index.html should be achieved this way:
<VirtualHost *:80>
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Important: ServerName should match your SSL certificate's Common Name (unless it's a wildcard certificate).
NB: RewriteEngine may be overkill for your purpose since the Apache mod_alias module provides the Redirect directive which may be sufficient here.
From the Apache documentation: When not to use mod_rewrite

Related

err_too_many_redirects when trying to redirect non www to www

Inside virtuahost apache conf file
sudo nano /etc/apache2/sites-available/000-default.conf
I tried to put these code inside apache conf file
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301]
also I tried,
< If "%{HTTP_HOST} != 'YOUR-DOMAIN.com'">
Redirect "/" "http://www.YOUR-DOMAIN.com/"
</If>
also did this
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real server configuration
</VirtualHost>
I checked also official apache https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost

htaccess redirect to a separate folder

Server is CentoS 7 with Apache
I have 2 folder under var/www/html/ which are
app/app_name/public (which contain laravel 4.2)
landing_page (which contain a simple index.html and a create.php file for subscription)
When you are accessing
domain.com -- this will open the landing_page/index.html
I would like to make it that when domain.com/beta is open, it will open up the request to the laravel application like:
var/www/html/app/app_name/public
I have tried virtual host which is
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/landing_page
ErrorLog /var/www/html/landing_page/error.log
CustomLog /var/www/html/landing_page/requests.log combined
Redirect /beta http://domain.com/app/app_name/public
</VirtualHost>
And .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Not sure what is the easiest but as long as it can make it work, should be good. Thanks.

Allow dynamic subdomain in url using htaccess

I have created a virtual host www.maindomain.com for my site. I want to allow dynamic sub domains in the url using htaccess i.e test.maindomain.com, abc.maindomain.com, etc should be allowed. My site folder structure is www/test/index.php.
Here is my current htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.maindomain\.com$ [NC]
RewriteRule .? index.php?url=%1&path=%{REQUEST_URI} [L,QSA]
</IfModule>
I get Server not found and could not connect error messages.
Can anyone suggest me a good solution for this issue?
Thanks in advance!
If your server is online:
You need a Wildcard DNS record plus a Wildcard serverAlias in your VirtualHost:
<VirtualHost *:80>
ServerAdmin admin#example.local
DocumentRoot "/my_server/htdocs"
ServerName example.local
ServerAlias *.example.local
# [...]
</VirtualHost>
Finally you will be able to catch those subdomains in your .htaccess file.

force https on subdirectory and redirect non-www traffic to www

So for the past couple hours I've been frustrated with my .htaccess file not working.
I have two subdirectories: secure.mysite.ca and storage.mysite.ca that need to be both ssl encrypted. The sites work when I put https in front of the URL (i.e. https://secure.mysite.ca/ or https://storage.mysite.ca/) but when I put http in front it redirects to my root website (http://mysite.ca/). I want to redirect to the https version of the site when someone tries to go to the unencrypted version.
ALSO I would like to redirect all traffic visiting http://mysite.ca/ to be redirected to http://www.mysite.ca/ (i.e. www in front)
This .htaccess file used to work over a year ago but now it doesn't.
Here is my htaccess file found in the root folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.ca$ [NC]
RewriteRule ^(.*)$ http://www.mysite.ca%{REQUEST_URI} [R=permanent,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Am I doing something wrong? Should I be putting an .htaccess file inside the subdirectory?
EDIT: Here is the virtualhost config file for secure.fixnode.ca, storage.fixnode.ca and www.fixnode.ca. Please let me know what I'm doing wrong
storage.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Online Storage"
<Directory "/var/www/fixnode_website/content/Online Storage">
allow from all
Options +Indexes
</Directory>
SSLEngine on
ServerName storage.fixnode.ca
SSLCertificateFile /path/to/cert/storage.crt
SSLCertificateKeyFile /path/to/private key/private.key
</VirtualHost>
secure.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Secure Login"
<Directory "/var/www/fixnode_website/content/Secure Login">
allow from all
Options +FollowSymlinks
</Directory>
SSLEngine on
ServerName secure.fixnode.ca
SSLCertificateFile /path/to/cert/cert.crt
SSLCertificateKeyFile /path/to/cert/mykey.key
</VirtualHost>
and finally my virtual host for my root domain www.fixnode.ca.conf
<VirtualHost *:80>
DocumentRoot /var/www/fixnode_website
<Directory "/var/www/fixnode_website">
allow from all
Options +Indexes
</Directory>
ServerAlias fixnode.ca
ServerName www.fixnode.ca
</VirtualHost>
If you have recently made a change to your system, you may have overwritten your apache config file(e.g. httpd.conf or apache2.conf) which may reset the options to default.
You need to place this rule in both the sub-directories of both sub-domains:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to automatically redirect HTTP to HTTPS on Apache servers?

Environment Centos with apache
Trying to setup automatic redirection from http to https
From manage.mydomain.com --- To ---> https://manage.mydomain.com
I have tried adding the following to my httpd.conf but it didn't work
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
Any ideas?
I have actually followed this example and it worked for me :)
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
Redirect permanent / https://mysite.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Then do:
/etc/init.d/httpd restart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
http://www.sslshopper.com/apache-redirect-http-to-https.html
or
http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html
Searched for apache redirect http to https and landed here. This is what i did on ubuntu:
1) Enable modules
sudo a2enmod rewrite
sudo a2enmod ssl
2) Edit your site config
Edit file
/etc/apache2/sites-available/000-default.conf
Content should be:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile <path to your crt file>
SSLCertificateKeyFile <path to your private key file>
# Rest of your site config
# ...
</VirtualHost>
Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.
3) Restart apache2
sudo service apache2 restart
Using mod_rewrite is not the recommended way instead use virtual host and redirect.
In case, if you are inclined to do using mod_rewrite:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same
location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context
Reference: Httpd Wiki - RewriteHTTPToHTTPS
If you are looking for a 301 Permanent Redirect, then redirect flag should be as,
R=301
so the RewriteRule will be like,
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add
Redirect permanent / https://[your-domain]/
I needed this for something as simple as redirecting all http traffic from the default apache home page on my server to one served over https.
Since I'm still quite green when it comes to configuring apache, I prefer to avoid using mod_rewrite directly and instead went for something simpler like this:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
SSLEngine on
...
</VirtualHost>
I like this because it allowed me to use apache variables, that way I didn't have to specify the actual host name since it's just an IP address without an associated domain name.
References:
https://stackoverflow.com/a/40291044/2089675
Server version: Apache/2.4.29 (Ubuntu)
After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz
To enable SSL, type (as user root):
a2ensite default-ssl
a2enmod ssl
In the file /etc/apache2/sites-available/000-default.conf add the
Redirect "/" "https://sub.domain.com/"
<VirtualHost *:80>
#ServerName www.example.com
DocumentRoot /var/www/owncloud
Redirect "/" "https://sub.domain.com/"
That's it.
P.S: If you want to read the manual without extracting:
gunzip -cd /usr/share/doc/apache2/README.Debian.gz
This code work for me.
# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
Please try this one in apache Virtualhosting configuration
and then reload apache service
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
for me this worked
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
This worked for me:
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]

Resources