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

3 Answers 3

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]
1
  • it is in httpd.conf file, I'm editing the file directly with vim. The first one worked, thanks!
    – jacktrades
    Jul 10, 2012 at 18:13
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/
3
  • the first one worked. please expand the redirect part, added the line t o httpd.conf and it did not work.
    – jacktrades
    Jul 10, 2012 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? Jul 10, 2012 at 18:42
  • main server config, yes I removed the mod_rewrite configs. I got: "This webpage has a redirect loop"
    – jacktrades
    Jul 11, 2012 at 14:46
1

How about change the line RewriteRule to:

RewriteRule ^/(.*)         http://example.com/$1 [last,redirect=301]
1
  • yes, that worked. refreshed the browser and it's working fine now.
    – jacktrades
    Jul 10, 2012 at 18:12

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