Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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]
share|improve this question

marked as duplicate by MadHatter, HBruijn, mdpc, HopelessN00b, Jenny D Jul 10 at 5:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 1 down vote accepted

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]
share|improve this answer
    
it is in httpd.conf file, I'm editing the file directly with vim. The first one worked, thanks! –  jacktrades Jul 10 '12 at 18:13

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/
share|improve this answer
    
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

How about change the line RewriteRule to:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.