In one of my projects I’m trying to redirect using .htaccess. I am using a redirect like this:
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
But, now I’d like to add an exception to the rewrite rules. I want http://example.com/admin/ to be redirected in the same way, only to a different directory. All of the other traffic should still go to the public directory. So i added a rewrite condition like this:
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This didn’t do the trick. I am still being redirected to the public folder. I also tried this:
RewriteRule ^admin$ admin/ [L]
RewriteRule admin(.*) admin/$1 [L]
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This only made things worse. I now get an 500 internal server error.
I have been searching for hours now, and I’m getting a bit tired of finding examples that show how to add an exception for a certain folder, but don’t show how to redirect this traffic to another folder. Can you tell me what I’m doing wrong, or which other approach I could try?
A
RewriteCondonly applies to the rule directly beneath it. In other words, in your example, it’s only applied to the empty path rule. Move theRewriteConddown one notch, and it will work better.Additionally, I find your rule
to be odd. A URL like
/adminsomethingwill be rewritten to/admin/somethingwhich I don’t think is what you want.Furthermore I have a hunch that you’re confusing a rewrite with a redirect. A rewrite is a rule that makes a request be rewritten internally and interpreted differently. A redirect is a rule that will tell the visitor’s browser to go to a different address. Which one are you going for?
The magic keyword for a redirect is
R=301which means redirect with http code 301 Moved Permanently. For example, you may want to try…