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]
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
jacktrades
  • 612
  • 3
  • 8
  • 15

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
  • 112,982
  • 12
  • 174
  • 248
  • the first one worked. please expand the redirect part, added the line t o httpd.conf and it did not work. – jacktrades Jul 10 '12 at 18:14
  • @jacktrades Are you using virtual hosts, or just the main server config? Did you remove the mod_rewrite config when you added the `Redirect` config? What occurred when you tried to access a page with it configured? – Shane Madden Jul 10 '12 at 18:42
  • main server config, yes I removed the mod_rewrite configs. I got: "This webpage has a redirect loop" – jacktrades Jul 11 '12 at 14:46
1

How about change the line RewriteRule to:

RewriteRule ^/(.*)         http://example.com/$1 [last,redirect=301]
Raymond Tau
  • 682
  • 3
  • 16
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
  • 25,847
  • 7
  • 57
  • 90