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>
siliconpi
  • 1,807
  • 6
  • 32
  • 46

4 Answers4

1

Try this one:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]
casper
  • 519
  • 2
  • 6
  • 12
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.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
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]
user842313
  • 851
  • 4
  • 6
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.

adaptr
  • 16,576
  • 23
  • 34