Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get a simple www. to non www. redirect set up. Here's the .htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

RewriteBase /

########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below

## This attempts to block the most common type of exploit `attempts` to Joomla!

## Deny access to extension xml files (uncomment out to activate)
#<Files ~ "\.xml$">
#Order allow,deny
#Deny from all
#Satisfy all

## End of deny access to extension xml files
# Block out any script trying to set a mosConfig value through the URL
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]

########## End - Rewrite rules to block out some common exploits

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

Accessing the page results in page not found. If I modify the top lines like this:

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

The redirect works great (it goes to

There are no RewriteCond's for in the apache httpd.conf file (where the virtualhosts are configured, specifically

apachectl -S:

VirtualHost configuration: (/etc/httpd/conf.d/ssl.conf:173) (/etc/httpd/conf/httpd.conf:1011)
Syntax OK

Relevant sections of each config: ssl.conf:


DocumentRoot /blah/blah/

ErrorLog /blah/blah/
CustomLog /blah/blah/
LogLevel warn
SSLEngine on
SSLProtocol all -SSLv2
SSLCertificateFile /blah/blah/
SSLCertificateKeyFile /blah/blah/
SSLCACertificateFile /blah/blah/

<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars

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"



    DocumentRoot /blah/blah/
    ErrorLog /blah/blah/
    CustomLog /blah/blah/


Help greatly appreciated!

share|improve this question
That config looks good. Could that domain be getting handled by a different <VirtualHost>, which isn't pointing to this .htaccess file's location? Check apachectl -S (and ensure that there's a ServerAlias in the relevant vhost). Also, just to confirm - the DNS for the www name is pointing to this server correctly, right? – Shane Madden Aug 10 '12 at 23:49
Just added output from apachectl -S, along with the relevant sections of each config. I did not find ServerAlias for – a coder Aug 11 '12 at 11:55
I added ServerAlias to both httpd.conf and ssl.conf. The domain appeared to redirect in Firefox 14.0.1, but checking in Chrome, the is not redirecting (same page not found as before). – a coder Aug 11 '12 at 12:11
Issued curl --verbose --head --location and got some results. curl --verbose --head --location results in 'couldn't resolve host' – a coder Aug 11 '12 at 12:14
Have you cleared your browser cache? – Michael Hampton Aug 11 '12 at 17:52
up vote 0 down vote accepted

There was not an entry in our DNS for Adding that solved the problem (or at least routed that traffic to my server, where I implemented the redirect above).

Thanks for the commentary.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.