Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a VPS on which I'm trying to setup multiple websites. For one of them, with the current set of rules (below), accessing

hxxp://example.com 

sends me to

hxxp://www.example.com// [note the two //]

My httpd.conf looks like:

NameVirtualHost *:80

[other VirtualHosts]

<VirtualHost *:80>
ServerAlias www.example.com example.com
DocumentRoot /var/www/html/example.com
ServerName www.example.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

<directory /var/www/html/example.com>

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9]+)(.*)/?$ /showitem.php?j=$1 [NC]

</directory>
</VirtualHost>
share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

Try this one:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]
share|improve this answer
    
Thanks casper!! –  matt74tm Jan 20 '12 at 14:07
    
+1 The Request URI includes the leading slash. –  Chris S Jan 20 '12 at 14:26
    
That would be a reason to use RewriteRule /(.*) and match it in the target, not the opposite. –  adaptr Jan 20 '12 at 17:50
add comment

The problem is that .* includes the slash that is already in the URL. You then send a redirect to http://www.example.com/$1 which includes another slash.

One of the following will fix your problem:

RewriteRule (.*) http://www.example.com$1 [R=301,L]

or

RewriteRule /(.*) http://www.example.com/$1 [R=301,L]

This will not happen inside a <Directory > block or in a .htaccess file because in both those cases there is no leading slash on the URI.

share|improve this answer
add comment

You can easily get rid of that double / by letting the regex part of the directive "eat" it:

RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
share|improve this answer
add comment

I've never heard of the hxxp protocol.

Your virtualhost answers to both example.com and www.example.com requests.

The fix is simple: Add a separate vhost for www.example.com and remove the ServerAlias from example.com.

Then Redirect permanent / http://www.example.com/

Note the prevalence of the slash on both source and target - that is the mistake you made.

I'm not going to show you how to fix the rewiterule, since you shouldn't be using it.

share|improve this answer
2  
Umm - that's because I'm a new user and making more than two links like asdf.com isnt allowed –  matt74tm Jan 20 '12 at 13:45
    
I don't know what that means. Don't use rewrites when anything else will do the job. –  adaptr Jan 20 '12 at 17:48
add comment

Your Answer

 
discard

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.