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.

Just starting to try to get the hang of how all the directives and options work under Apache.

I'd like to do a redirect with my one site (only running one site on the server) so that when a request comes in to http:/example.com the server automatically redirects them to a sub-url of http://example.com/mylink.

I have tried putting redirects into the file located in /etc/apache2/sites-enabled to rewrite this, but then the top level domain URL complains it isn't redirecting properly.

I think what I want is a browser redirect, and thought using

RewriteEngine On
RewriteRule ^/$ /mylink [L,R]

would work, but putting it into an .htaccess file didn't work (it redirected but immediately gave a 500 internal server error.)

Putting it into the file in /etc/apache2/sites-enabled gives a configuration error when trying to restart Apache.

I know it's something simple...but what am I missing?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

Try a RedirectMatch, instead:

RedirectMatch permanent ^/$ http://mysite.com/mylink

They're often easier when you're after a simple redirect, whereas Rewrites come into their own when you don't want the end user to see the results.

share|improve this answer
1  
Worked when I put it into the /etc/apache2/sites-enabled/sitefile! Thanks! –  Bart Silverstrim Jun 30 '11 at 15:42
    
+1 for the simple solution. @Bart If you do try to use mod_rewrite in the future and have similar issues, you'll need to be more forthcoming with the details, such as what's in the error log; 500 errors on untouched URLs don't add up. –  Shane Madden Jun 30 '11 at 16:02
    
You already touched on the .htaccess not working, @Shane. The only thing in the error log was saying that RewriteEngine is an invalid command in .htaccess. The other attempts showed nothing in the error log. –  Bart Silverstrim Jun 30 '11 at 16:06
    
@Bart To clarify, it'll work just fine in .htaccess, but it expects relative paths when they're used in that context (or <Directory>). I know your issue is solved (and the solution is better than using mod_rewrite, for sure) - just trying to clear up misconceptions on mod_rewrite for if/when you do need to use it, since as you said, you're just starting to get the hang of this stuff. –  Shane Madden Jun 30 '11 at 16:23
    
Thanks @Shane! Appreciate the clarification. –  Bart Silverstrim Jun 30 '11 at 18:06
add comment

Where are you putting it in the site file? That should work just fine in the <VirtualHost> context, but not in the .htaccess file or <Directory> context (those expect relative, not absolute, paths).

Then again, they shouldn't blow up the config or cause 500 errors, either - just not redirect. Should be something interesting in error.log.

When you say it redirected then 500 errored, the 500 came back on the "new" URL? Does it 500 when you go directly to it, avoiding the redirect?

share|improve this answer
    
I tried it in the virtualhost block and in an .htaccess file. Yes, the 500 error came from the new url. –  Bart Silverstrim Jun 30 '11 at 15:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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