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>
Related
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.
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}
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
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.
I want, when open subdomain to return existing Wordpress page ex. www.example.com/custom-wordpress-page.
Server OS is CentOS 6.6, while Web Server (httpd) is Apache 2.2.15.
It have installed and configured Wordpress with .htaccess files configured for it's mod_rewrite.
Root folder is /var/www/html.
.htaccess file is in /var/www/html/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This configuration is working very well with one domain (ex. example.com). Today I wanted to add subdomain (ex. subdomain.example.com).
I tried multiple solutions including VirtualHosts with/without NameVirtualHost in httpd.conf file, also adding RewrriteCond&RewriteRule
under mod_rewrite in .htaccess file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Redirect permanent / http://example.com/custom-wordpress-page
</VirtualHost>
Also in first VirtualHost, I tried *.example.com instead off www.example.com.
In second one, I tried to make Reverse Proxy using ProxyPass and ProxyPassReverse.
Instead of using :*80 I tried using IP address of server and domain name.
Using .htaccess Rewrite rule with/without VirtualHosts gave no results.
subdomain.example.com always opens www.example.com instead of www.example.com/custom-wordpress-page.
Keep in mind - there is a lot of threads like this, but I tried many of them, including many tutorials but they aren't working alongside Wordpress.
Edit : Want to add that there are no wildcards in DNS records. There are NS records, one A record for example.com, one A record for subdomain.example.com and CNAME record for www that is poiting to example.com. There are NO *.
There was problem with hosting provider DNS records, configuration was correct, but I had to call provider to check records in their system and fix what is broken. After they done their job, it works correctly.