0

When trying to redirect an URL in apache from:

www.example.com

to

example.com

it redirects to

example.com//

This is a one page webserver, Fixed IP goes directly to the page.

Editing directly on httpd.conf

Any idea of why is this happening?

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

3 Answers3

2

Where is that rule configured?

Seems like it's probably in the server or virtualhost configuration, where it would have a leading slash in the match string, which is being captured and replaced into the redirect string.

Try this:

RewriteRule ^/(.*) http://example.com/$1 [last,redirect=301]

It doesn't seem like you need to be using mod_rewrite for this, if that's all the complexity you need. If your rules don't need to be any more complex than this, use Redirect instead:

Redirect 301 / http://example.com/
Shane Madden
  • 116,404
  • 13
  • 187
  • 256
1

How about change the line RewriteRule to:

RewriteRule ^/(.*)         http://example.com/$1 [last,redirect=301]
1

Is this in a .htaccess file or a <Directory /> block? My guess is that it is not.

Inside those two areas, the URI path is matched to a full filesystem path and the the part that matches the directory, including the slash, is removed.

Outside one of these areas, the URI path starts with a slash. Hence, your redirect puts an extra slash in.

You can change the rewrite rule to either:

RewriteRule ^/(.*)         http://example.com/$1 [last,redirect=301]

Or

RewriteRule ^(.*)         http://example.com$1 [last,redirect=301]
Ladadadada
  • 27,207