Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: How to force redirect to a non-www domain |
|
Author |
|
ychaikin
Joined: 23 May 2013 Posts: 2 Location: USA, Baltimore
|
Posted: Thu 23 May '13 20:32 Post subject: How to force redirect to a non-www domain |
|
|
Hi,
On a default installation of CentOS (on AWS), running apache 2.2, I have the following configuration in my conf.d directory. So, besides for this configuration, I just have the default conf/httpd.conf file.
What I am trying to achieve is a configuration that:
1) If URL starts with http://www.example.com, it goes to http://example.com
2) If URL starts with https://www.example.com, it goes to https://example.com
I was able to achieve #1 with the configuration below. However, to achieve #2, I have configuration that is currently commented out at the end (what I provided below). If I un-comment it, the server fails to start with the error message:
Code: | Starting httpd: [Wed May 22 05:01:57 2013] [warn] default VirtualHost overlap on port 443, the first has precedence. |
I am a bit confused as I thought the docs said that default is just an alias for '*'. If I change both port 443 VirtualHost entries to say *:443, the server start up still fails with the same error as above. However, if I change both port 443 VirtualHost entires to say _default_:443, the server starts up, but https://www.example.com/ request stays the same and no redirect happens.
Any ideas what I am doing wrong here and how to fix it would be highly appreciated!
Thank you!
-Yaakov.
Here is the configuration:
Code: |
LoadModule ssl_module modules/mod_ssl.so
Listen 443
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html/example.com
ServerName example.com
CustomLog "/var/log/httpd/example.com_log" common
ErrorLog "/var/log/httpd/example.com_error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
SSLPassPhraseDialog builtin
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost _default_:443>
ServerName example.com
DocumentRoot /var/www/html/example.com
ErrorLog logs/example_ssl_error_log
TransferLog logs/example_ssl_access_log
LogLevel warn
SSLEngine on
...
SSLCertificateFile /etc/pki/tls/....
SSLCertificateKeyFile /etc/pki/tls/....
SSLCertificateChainFile /etc/pki/tls/....
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
# VirtualHost to redirect to non-www domain
#<VirtualHost *:443>
# ServerName www.example.com
# Redirect permanent / http://example.com/
#</VirtualHost>
|
|
|
Back to top |
|
James Blond Moderator

Joined: 19 Jan 2006 Posts: 7404 Location: EU, Germany, Next to Hamburg
|
Posted: Fri 24 May '13 10:15 Post subject: |
|
|
You need two rewrite rules. One in each vhost
for the port 80
Code: |
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
|
for 443
Code: |
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
|
|
|
Back to top |
|
ychaikin
Joined: 23 May 2013 Posts: 2 Location: USA, Baltimore
|
Posted: Fri 24 May '13 15:44 Post subject: |
|
|
Thank you! |
|
Back to top |
|
|
|
|
|
|