Rewrite rules all variants of one main domain to another - wordpress

Say I'd like all traffic from one web domain to be redirected to another. Both sites are Wordpress sites. What's the best way to do this? By including rewrite rules in .htaccess?
For example -
domain1.com/(anything) --> domain2.com
subdomain.domain1.com/(anything) --> domain2.com
If .htaccess, can someone give me some pointers as to what the rule might look like?
Thanks.

You can do this with a RewriteRule. You want to put this inside a VirtualHost block. Example:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName domain1.com
ServerAlias *.domain1.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteRule ^(.*)$ http://domain2.com$1 [R]
</VirtualHost>
This will take anything to the right of domain1.com and any subdomain of domain1.com and issue a 301 redirect to http://domain2.com/what/ever/path/was/requested

Related

Redirect Server Aliases to Server Name Over HTTPS on Apache

I have been looking around for an answer to my problem for a while and have read various other answers without coming across one that works for me.
The problem I am trying to solve involves redirecting access to a WordPress site installed on an Apache server. The WordPress site is one of many installed as part of a WordPress Network installation and the site is setup on the domain https://www.mysite1.co.uk.
It is also necessary to access the site using a number of server aliases, which are to be redirected to the root domain. I have created the additional domains as server aliases in the Apache configuration, and have set redirects to ensure that if one of the aliases is entered then it should be redirected to the root domain.
<VirtualHost *:80>
ServerName www.mysite1.co.uk
ServerAlias mysite1.co.uk www.mysite2.co.uk mysite2.co.uk
...
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.mysite1.co.uk%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.mysite1.co.uk
ServerAlias mysite1.co.uk www.mysite2.co.uk mysite2.co.uk
...
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.mysite1.co.uk%{REQUEST_URI} [R=301,L]
SSLEngine On
SSLCertificateFile "certs/mysite1.co.uk.crt"
SSLCertificateKeyFile "certs/mysite1.co.uk.key"
</VirtualHost>
This configuration works whenever the user enters one of the aliases using http, they are redirected to https://www.mysite1.co.uk/. However, if they enter the aliases using https, instead of being redirected to https://www.mysite1.co.uk/, they are not being redirected at all.
Have I missed something in the configuration, or do I need to separate the https configuration and add the aliases into their own virtual host to do the redirect.
The sites are hosted on CentOS 7.9.2009 and running Apache 2.4.6.
If I got it correctly, you're trying to redirect every known domain reaching your apache to just one domain and when the connection comes as HTTP you want to redirect it to HTTPS.
To keep the configuration simple, I would create 3 different virtual hosts:
The first one will listen on http and include every domain you want to redirect (including the main one becuase it's listening on http). This virtualhost will redirect everything to the main domain over https:
<VirtualHost *:80>
ServerName main.site.com
ServerAlias mysite.example.com
ServerAlias another.site.com
Redirect permanent / https://main.site.com/
</VirtualHost>
The second one will listen on https port and includes all the domains but the main one and redirect it to your primary domain (it's like the previous one except it doesn't include the main domain)
<VirtualHost *:443>
ServerName mysite.example.com
ServerAlias another.site.com
Redirect permanent / https://main.site.com/
...
SSLEngine On
...
</VirtualHost>
In the end, the main one will just listen on https and will include only the main domain:
<VirtualHost *:443>
ServerName main.site.com
...
SSLEngine On
...
</VirtualHost>
This way, the only virtualhost requiring configuretiona (document root, locations, etc.) is the last one.

Redirect HTTP domain to HTTPS domain via htaccess file code

Please let me know how I can redirect http domain to https domain hassle free via htaccess file. I want to redirect this domain
http://example.in to https://example.com
domain.
Please help me out.
Thanks in advance!!!
Apache doesn’t recommend to use this in the .htaccess, you can read about it here.
You should use this instead:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
In case that you want to use .htaccess anyway you need to use the next rewrite:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Have subdomain act as root (htaccess?)

I have a website, with some content in the root folder.
I then have a Wordpress site in a subfolder of a subdomain. So for example the url to the Wordpress 'contact' page looks like this.
subdomain.mywebsite.com/subfolder/contact/
Is there a way to get this Wordpress site to act as the root of the website without moving files around? Either through an htaccess file or any other method?
It would be important that the user doesn't know they are in a subdomain/subfolder, so for the contact page of the wordpress site the url bar would have to look something like:
www.mywebsite.com/contact/
Would really appreciate any help.
You can do that in the vhost entry of your webserver.
Example main entry:
<VirtualHost *:80>
ServerAdmin xxx#xxx.com
DocumentRoot "c:/Users/xxx/Documents/www/"
ServerName xxx.com
ErrorLog "logs/xxx.error.log"
CustomLog "logs/xxx.access.log" common
</VirtualHost>
Example subdomain entry:
<VirtualHost *:80>
ServerAdmin xxx#xxx.com
DocumentRoot "c:/Users/xxx/Documents/www/subfolder"
ServerName subdomain.xxx.com
ErrorLog "logs/subdomain.xxx.error.log"
CustomLog "logs/subdomain.xxx.access.log" common
</VirtualHost>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -f
RewriteRule ^([^/]+)/?$ /subfolder/$1 [L]
This will internally map /file/ to /subfolder/file if the requested file exists in /subfolder.

hide subdomain in address bar

My main website is located on www.example.com. On the subdomain sub.example.com I'm building another website. Both websites are build with WordPress. Both domains are registred at the same registrar
I registered a domainname (lets say: www.example2.com) wich I would like to point to sub.example.com. When someone visites www.example2.com he/she should see www.example2.com in the address bar and not sub.example.com.
I was trying to get this to work with a .htaccess file and it kind of worked, but not in the way I was hoping:
when I enter example2.com the browser opens sub.example.com and in the address bar it states example2.com (so far, so good)
when I navigate to another page on the website the address bar shows sub.example.com/another-page instead of example2.com/another-page
when I enter www.example2.com the browser opens www.example.com
This is how my .htaccess file looks like at the moment:
RewriteEngine On
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteCond %{HTTP_HOST} www.example2.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com [R=301,L]
Can someone tell me how I can get this to work?
Try this and see if it works for you. Also I would make sure the www dns record for example2.com is pointing to the same location as example2.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [L]
Edit:
Really for what you are trying to accomplish, I would suggest NameBased virtual Hosts if possible. That's what it was designed for. So that you can use the same IP for multiple sites. This change is done in the apache config file. See this simple example.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /www/example.com/html
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
ServerAlias example2.com
DocumentRoot /www/example.com/html/sub
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/name-based.html
That tells the server what document root to use for each website and therefore eliminates the need for these mod redirects and CNAMEs to sub domains.

Apache2 configuration causing redirect loop

I'm trying to setup my site to use www only, and non-www should be permanently redirected to www. This answer suggested using two virtual hosts, however it causes me to go into a redirect loop.
Here's the configuration for the site:
<VirtualHost *:80>
ServerName www.mydomain.com
DirectoryIndex index.html index.php
DocumentRoot /home/me/sites/mydomain.com/htdocs
# Log file locations
LogLevel warn
ErrorLog /home/me/sites/mydomain.com/logs/error.log
CustomLog /home/me/sites/mydomain.com/logs/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain.com
Redirect permanent / http://www.mydomain.com/
</VirtualHost>
When I visit the non-www version of the site it successfully redirects to the www version, however Chrome then tells me that there was a redirect loop.
At first I thought it could be .htaccess in my document root however after removing that file it still happens. It's just a simple Wordpress site.
Can anyone see something wrong with the config that would cause this to happen? If not, how can I narrow down the cause?
You don't need a separate VirtualHost entry for non-www and use ServerAlias instead. Also to redirect to www just use a rule like this:
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
DirectoryIndex index.html index.php
DocumentRoot /home/me/sites/mydomain.com/htdocs
# Log file locations
LogLevel warn
ErrorLog /home/me/sites/mydomain.com/logs/error.log
CustomLog /home/me/sites/mydomain.com/logs/access.log combined
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

Resources