I have a apache2 serving an application which works fine on www.domain.co.uk but when going to domain.co.uk (non www) it doesn't redirect the traffic. I added a redirect to my .conf file and it still doesn't work. See below:
<VirtualHost *:80>
ServerName domain.co.uk
Redirect permanent / http://www.domain.co.uk/
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.co.uk
ServerAlias domain.co.uk
# Actual server configuration
</VirtualHost>
Does anyone know how to redirect the non-www traffic to www (or fix my configuration!)?
You have two separate areas of the configuration capturing domain.co.uk - one is in the redirection, the other (through your use of ServerAlias) in the configuration that serves the content. The second capture is overriding the first.
To resolve the issue, simply remove the line:
ServerAlias domain.co.uk
and restart Apache.
Related
I'm setting up a Virtual Hosts file on my CentOS 7 box and I'm having trouble getting my domain to resolve correctly.
Here's what my current /etc/httpd/conf.d/vhost.conf file looks like
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/domain.com/public_html/
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
It seems the the correct redirects are happening. For exmaple:
domain.com redirects to https: //www.domain.com
www works fine
BUT
https: //domain.com doesn't work
http ://domain.com doesn't work
In fact, if I remove the redirects I have set, domain.com ins't working at all, so it looks like the ServerAlias is broken?
I'm wondering if I need another redirect or is there some other step I'm missing?
Also, don't mind the spaces between http and the domain name. StackOverflow made me format it that way.
As presented, no request to anything https will ever work. Normal, you only have a VirtualHost on port 80. You do have a Listen directive for that port right?
For your redirections. It says: if you ask for http://www.example.com or http://example.com, redirect to https://<WHAT THE USER ASKED FOR>. In essence you are forcing your users to use https all the time, no problem there. But you do not have a VirtualHost on port 443, hence no response.
So:
Listen *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ErrorLog /var/log/httpd/80_error.log
CustomLog /var/log/httpd/80_access.log combined
RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
Listen *:443
<VirtualHost *:443>
ServerName www.example.com
# in case users do directly to https
ServerAlias example.com
DocumentRoot /var/www/html/domain.com/public_html/
DocumentIndex index.html
ErrorLog /var/log/httpd/443_error.log
CustomLog /var/log/httpd/443_access.log combined
# SSL CONFIGURATIONS, TODO!
</VirtualHost>
In your *:443 VH, you will have to configure certificates and SSL.
Your certificates will have to be valid for both www.example.com and example.com to avoid browser complaints.
Careful there might be an ssl.conf included file under conf.d that defines some of this. Make sure you only set it once to avoid confusion.
No need to define DocumentRoot in *:80 VH since it only redirects and does not respond content to client.
Have fun!
I solved the issue. I had my local hosts file configured to point to an old out of date IP address……
domain.com *bad ip address*
I'm so embarrassed. I must have set that up months ago and forgot.
How can I force HTTPS on my website? I tried the solution from this answer, but for some reason I get redirected to the parent directory when I visit the index page by clicking on a link (it worked fine before adding those lines of code in .htaccess). Also, when I try to visit my website using HTTP, it lets me do it. What am I missing?
With Apache, you have several alternatives - including .htaccess.
Look here:
https://wiki.apache.org/httpd/RedirectSSL
Per the documentation, your best bet is to use a Redirect directive inside the non-secureVirtualHost:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Here is an .htaccess example, from the same link:
Redirect permanent /login https://mysite.example.com/login
Finally, look here for additional troubleshooting tips (for example, forgetting 'NameVirtualHost *:443' to enable Named virtual hosting for port 443):
Why might Apache ignore a virtual host with a ServerName matching the requested URL?
I have installed an SSL certificate today, and it works perfectly, pages sought through https:// work fine.
I have put a redirect in my website.conf to redirect port 80 to https, but it is not working.
Also if I go to http://www.example.org (the root), it gets stuck in a redirect loop.
going to http://www.example.org/test.htm goes to the same url minus the last /. eg http://www.example.orgtest.htm/ (slash moves to end)
This is really busting my balls, i really hope you guys have some clues.
here is my .conf file
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.org
Redirect permanent / https://www.example.org
</VirtualHost>
<VirtualHost _default_:443>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin richard#example.org
ServerName example.org
ServerAlias www.example.org
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.org
DocumentRoot /var/www/example.org/html
# SSL settings
SSLEngine on
SSLCertificateFile /etc/ssl/certs/www_example_org.crt
SSLCertificateKeyFile /etc/ssl/digicert/example.key
SSLCACertificateFile /etc/ssl/certs/DigiCertCA.crt
# Log file locations
LogLevel warn
ErrorLog /var/www/example.org/log/error.log
CustomLog /var/www/example.org/log/access.log combined
</VirtualHost>
Your config looks fine for me. Maybe, you should append a / like this: Redirect permanent / https://www.example.org/
Instead of using Redirect permanent, you could also use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
For this to work, you must have installed the mod_rewrite module.
If you would like for force https, another approach is so set the HSTS-Header in your Apache config. Then, a browser knows that your website is available with https and will automatically redirect all traffic to your https-address. You can set this header with the following command:
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
In general, I would recommend reviewing your SSL-config with SSLLabs. If your config is fine, you should get an A+-Rating. I hope this helps.
well i worked it out after much tears, i was missing
ServerAlias www.example.org
for port 80, Thanks to Ben for the HSTS header though, i will def use that. cheers
I know there are plenty of articles here talking about almost equal questions. However I couldn't fix it yet.
We have a normal wordpress domain
domain.com
I have two virtual hosts, one to http and another to https, from http virtual host I make a redirect to force the https always
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
Redirect permanent / https://domain.com
</VirtualHost>
then my https virtualhost is something like
<VirtualHost *:443>
ServerName domain.com
DocumentRoot /var/www/vhosts/puntdona.com/httpdocs
....
</VirtualHost>
Problem:
every time I enter into navigator something like
https://www.domain.com
navigator (Google,etc) complains about it because the certificate (chepeast one) just work to non www urls. I've tested lot of setups to try to redirect the
https://www.domain.com to http://domain.com
but didn't work.
I want to get that Apache redirects https://www -> to https:// (without www).
But I don't know how.
Thanks
You just can't redirect to mentioned url. To redirect you need to have different url.
For reference visit official apache docs -
http://httpd.apache.org/docs/2.4/rewrite/remapping.html
I am running an Apache HTTP server that accepts requests for my domain. Lets call it www.mydomain.com. I have several sites that run under this domain and I have virtual hosts set up for each one that forwards the user depending upon with URL they use. Examples would be a.mydomain.com, b.mydomain.com, etc. I am aware that this may not have been the best way to accomplish this goal considering there are over 100 virtual hosts, but it is something I have inhertited and.. to put it short.. it works. Now on to my problem.
I have recently been tasked with shutting down most of the sites running under the domain. The sites that are shut down have been redirected to one page on my new domain (www.mynewdomain.com). What I have done is changed each of the virtual hosts from something like this:
<VirtualHost *:80>
ServerName a.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteA [PT]
</VirtualHost>
to this:
<VirtualHost *:80>
ServerName a.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
So now I have a list of 100 hosts that all redirect to the same place. What I need to know is if there exists a way to tell the system to redirect all sites at *.mydomain.com to my new page except certain ones. So some entry like this:
<VirtualHost *:80>
ServerName *.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
<VirtualHost *:80>
ServerName h.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteH [PT]
</VirtualHost>
<VirtualHost *:80>
ServerName v.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteV [PT]
</VirtualHost>
Which means that every URL you see will go to "http://www.mynewdomain.com/replacement" except "h.mydomain.com" and "v.mydomain.com". The virtual hosts for "h" and "v" must still be intact because I have RewriteRules for each that must continue to work (additional rewriterules not seen in my examples).
Thanks in advance for the assistance!