Apache VirtualHost redirect, change only domain - apache

How can I create VirtualHost to redirect all links changing only its domain, including subdomain and parameters:
exampleA.com -> exampleB.com
test.exampleA.com -> test.exampleB.com
test1234.exampleA.com/url/test.html?param=222 -> test1234.exampleB.com/url/test.html?param=222
I want to redirect all subdomains like *, and it should be permanent 301
Now I have a simple 301 redirection
<VirtualHost *:80 *:443>
ServerName exampleA.com
ServerAlias *.exampleA.com
RewriteEngine On
Redirect 301 / https://exampleB.com
</VirtualHost>

I have never done something like this, but try the redirect option in your virtualhost file. First enable rewrite
sudo a2enmod rewrite
Then in your virtualhost file
RewriteEngine on
RewriteCond %{SERVER_NAME} =exampleA.com [OR]
RewriteCond %{SERVER_NAME} =www.exampleA.com
RewriteRule ^ https://exampleB%{REQUEST_URI} [END,NE,R=permanent]
Read more about this here: https://httpd.apache.org/docs/2.4/rewrite/remapping.html
It takes the original domain and rewrites to another. In older apache I remember it goes something like this:
<VirtualHost *:80>
ServerName www.domain1.com
Redirect / http://www.domain2.com
</VirtualHost>

I found the solution:
<VirtualHost *:80 *:443>
ServerName exampleA.com
ServerAlias *.exampleA.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.+\.)?exampleA\.com$ [NC]
RewriteRule (.*) https://%1exampleB.com$1 [R=301,L]
</VirtualHost>

Related

Wildcard SSL vhosts configuration with RewriteEnginer

I am currently working to configure a domain that has many wildcard subdomains. I want to make sure the following occurs.
All *:80 traffic gets translated to the corresponding fqdn domain name in HTTPS
For Example:
http://jane.example.com -> https://jane.example.com
http://jack.example.com -> https://jack.example.com
http://www.example.com -> https://www.example.com
A couple caveats:
A. my ssl cert is a wildcard, so if there is no host, I want to make sure the redirect includes a host of www. both on *:80 and *:443
http://example.com -> https://www.example.com
https://example.com -> https://www.example.com
The vhost looks like:
<VirtualHost 108.161.x.x:443>
Servername %1.example.com
ServerAlias www.example.com
DocumentRoot /home/sites/example.com/www
ErrorLog logs/md-ssl-error_log
CustomLog logs/md-ssl-access_log common
SSLEngine on
SSLCertificateFile /etc/ssl/kl_crt.crt
SSLCertificateKeyFile /path/to/kl_pk.key
SSLCertificateChainFile /path/to/kl_cab.crt
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>
The regular http traffic here
<Virtualhost 108.161.x.x:80>
ServerName %1.example.com
RewriteEngine On
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>
Anyone know whats wrong with my code?
You can try something like this :
ServerName example.com
ServerAlias %1.example.com
DocumentRoot /home/sites/example.com/%1

Redirect http://www. old path to https://www. new path (1to1 redirect)

Struggle with Redirect http://www. to new path and https://www.
Since we want to force https:// it creates a chain from http://www. old path to https://www. old path to https://www. new path
http://www. old path -> https://www. old path -> https://www. new path
301 -> 301 -> 200
Anyone who can save my day? How do we solve this in .htaccess (apache)?
If you have a new domain name:
<VirtualHost *:80>
ServerName www.example-old.com
Redirect 301 / https://www.example-new.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.example-new.com
DocumentRoot /your_document_root
SSLEngine on
...
</VirtualHost>
If you want to redirect from one folder to another:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
Redirect 301 /old-path https://www.example.com/new-path
DocumentRoot /your_document_root
SSLEngine on
...
</VirtualHost>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} wp-includes
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} old-page
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>
This setup works, but how do I add more URI's to do it in scale instead of adding 20-50 's?
How do URI for index/home page look? Since it's the root /. What would be instead of wp-includes or old-page as folder there?

httpd virtualhost - redirect domains not in the list

I'd like to do a redirect when a subdomain requested is out of scope.
Example of my subdomains:
www.vm.com
client.vm.com
agent.vm.com
employee.vm.com
admin.vm.com
VirtualHost block used:
<VirtualHost *:80>
DocumentRoot /home/apache/projects
ServerName vm.com
ServerAlias *.vm.com
ErrorLog logs/vm.com-error_log
CustomLog logs/vm.com-access_log common
</VirtualHost>
I can't find any negation example, only intentional redirects like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub1\.vm\.com [NC]
RewriteRule (.*) http://www.vm.com$1 [R=301,L]
Is it possible to have some kind of syntax like:
!(www|client|agent|employee|admin)\.vm\.com
Thanks.

How to redirect a web page while not changing the URL (using Apache VirtualHost)

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.

htaccess https for several subdomains

I have virtual hosts
<VirtualHost 10.10.10.10:80>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
<VirtualHost 10.10.10.10:443>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
I want to force visitors use https for subdomain-a and subdomain-c. Visitors of subdomain-b and subdomain-d could use http and https. How to configure .htaccess?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(subdomain-a|subdomain-c)\.server\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]