0

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>

4 Answers 4

1

Try this one:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]
2
  • +1 The Request URI includes the leading slash.
    – Chris S
    Jan 20, 2012 at 14:26
  • That would be a reason to use RewriteRule /(.*) and match it in the target, not the opposite.
    – adaptr
    Jan 20, 2012 at 17:50
1

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.

1

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]
0

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.

2
  • 2
    Umm - that's because I'm a new user and making more than two links like asdf.com isnt allowed
    – siliconpi
    Jan 20, 2012 at 13:45
  • I don't know what that means. Don't use rewrites when anything else will do the job.
    – adaptr
    Jan 20, 2012 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .