I have a website that is heavily made up of subdomains. I want to redirect all subdomains to their HTTPS counterpart but I'm not sure how to do that in my vhost
http://subdomain1.example.com/ -> https://subdomain1.example.com/
http://subdomain2.example.com/ -> https://subdomain2.example.com/
...
http://example.com/ -> https://example.com/
Adding the following code should solve your issue.
<VirtualHost *:80>
ServerName subdomain1.example.com
...
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName subdomain1.example.com
...
</VirtualHost>
Note: check that mod_rewrite is enabled
Related
I have an issue redirecting from the http site to the https site.
This index.php file is in the root folder, redirecting to a suburl:
<?php
header("Location: /news/");
?>
the url https://www.example.com is correctly redirecting to https://www.example.com/news/
the url https://www.example.com/index.php is correctly redirecting to https://www.example.com/news/
the url http://www.example.com/index.php is correctly redirecting to https://www.example.com/news/
however the url http://www.example.com is showing an empty directory, as in the image below:
these are the config files:
/etc/apache2/sites-available/www.example.com.conf
<VirtualHost *:*>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
/etc/apache2/sites-available/www.example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
How can I correctly redirect from http://www.example.com to https://www.example.com/news/ ?
One has two options -- Virtual Host Redirection, and Request Rewriting. Adapt the following configuration fragments to your needs.
Virtual Host Redirection
To be prefered as being simpler and safer. Redirect an HTTP virtual host (Port 80) to a HTTPS virtual host (Port 443) via the virtual hosts configuration:
# Listening for HTTP connections
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
# permanently redirects to the site's HTTPS version
Redirect permanent / https://www.example.com/
</VirtualHost>
# Listening for HTTPS connections
<VirtualHost _default_:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# Further configurations...
</VirtualHost>
Request Rewriting
Alternatively -- usually, when the virtual hosts config is not accessible -- one can edit the .htaccess file (create it, if necessary) to rewrite HTTP to HTTPS requests. For that the module mod_rewrite has to be enabled, so
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf has to be set (and usually is, by default). Then add to the .htaccess file:
# Enable rewriting
RewriteEngine On
# Check for HTTPS, if no, execute next line
RewriteCond %{HTTPS} off
# Redirect to HTTPS with status code 301 (moved permanently)
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
I used:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualHost>
Type the code into the conf file.
I placed the following config in apache site conf file:
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
ServerAdmin webmaster#localhost
Redirect permanent / https://domain.com/
</VirtualHost>
How can I have subdomains redirect without going to the main domain?
If I type http://subdomain.domain.com/ it redirects to https://domain.com/ but I want it to redirect to https://subdomain.domain.com/. I also want it to be a wildcard so that it works for all subdomains.
Is it possible without engaging the mod_rewrite module?
I don't think it's possible without mod_rewrite, but I'm not sure. With rewrite is very simple though:
RewriteEngine On
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have an Apache server hosting my HTML site. I have it setup currently to be SSL-enabled on www.mysite.com. It will also redirect from http://example.com to https://www.example.com.
However, I am having two issues:
First, I cannot figure out how to redirect:
http://www.example.com to https://www.example.com (not the ssl there)
Second, the www redirect also doesn't occur I navigate to:
https://example.com/
Below is the relevant portions of my httpd.conf file.
<VirtualHost *:80>
ServerName default
ServerAlias *
<IfModule mod_rewrite.c>
RewriteEngine on
# WITH 'www.'
RewriteCond %{HTTP_HOST} !^www.(.*) [nocase]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [redirect=permanent,nocase,last]
</IfModule>
</VirtualHost>
<VirtualHost *:443>
ServerName www.exampe.com
SSLENGINE on
SSLCertificateFile /etc/pki/tls/signed/my.crt
SSLCertificatekeyFile /etc/pki/tls/signed/my.key.csr
SSLCertificateChainFile /etc/pki/tls/signed/my.ca-bundle
</VirtualHost>
Does anyone have any idea how to modify the above VirtualHost elements to include the https redirect (bullet point 1) and have the redirect enabled for the ssl-enabled, non-www form of the url (bullet point 2)?
For first case remove the IfModule and mod_rewrite mess and add this in the non-SSL virtualhost:
Redirect / https://www.example.com/
For Second case add this in the SSL VirtualHost:
UseCanonicalName on
I'm setting up Virtual Hosts in my Apache web server and already have rules in place that just do a simple 301 redirect from one URL to another. However, I've now been asked if I can write a rule that redirects to another page while keeping the URL the same and I've tried this:
<VirtualHost *:80>
ServerName ZZZ.com
ServerAlias YYY.com ZZZ.com
Redirect / YYY.com
</VirtualHost>
And this:
<VirtualHost *:80>
ServerName ZZZ.com
RewriteEngine on
RewriteRule ^/(.*) YYY.com/$1 [R]
</VirtualHost>
Neither did what I expected of them. They look wrong to me but I'm just not finding any helpful information anywhere. I've looked at http://httpd.apache.org/docs/2.4/vhosts/examples.html and http://httpd.apache.org/docs/current/mod/mod_rewrite.html - Neither of them were very helpful.
<VirtualHost *:80>
ServerName YYY.com
ServerAlias ZZZ.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^yyy\.com$ [NC]
RewriteRule (.*) YYY.com$1 [R=302,L]
</VirtualHost>
You were missing a condition to prevent it from redirecting correct urls.
I've got a VirtualHost that looks something like:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com domain2.com www.domain2.com
</VirtualHost>
When someone visits www.domain1.com/test, they should be redirected to:
domain1.com/test
When someone visits www.domain2.com/test, they should be redirected to:
domain2.com/test
My current RewriteRules are lacking.
Edit: Here's what I've got so far:
# Rewrite www to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} www\.%{HTTP_HOST}$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
Obviously, this generates an infinite redirect loop.
No need for rewrites.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias domain2.com
... real vhost settings ...
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain1.com
Redirect permanent / http://domain1.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
Redirect permanent / http://domain2.com/
</VirtualHost>
Your RewriteCond is a bit wonky. I'm surprised it does anything at all, since it would seem to be trying to match the host www.domain1.com against the pattern www\.www.domain1.com. These directives worked for me:
# Redirect www to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [L,R=301]
You can have multiple VirtualHosts in a configuration file, so you should change your config to this:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
You can add another VirtualHost for each domain you want to do.