162

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.

Directories to exclude: "admin" and "user".

So http requests: http://www.domain.com/admin/ should not be passed to index.php file.

ErrorDocument 404 /index.php?mod=error404

Options  FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
2
  • I think you forgot the quantifiers after [^/] since a plain space if not allowed there (it must be escaped with \<space>). Commented Dec 4, 2009 at 17:54
  • True Gumbo, good catch :). it should be ^([^/]+). Commented Dec 4, 2009 at 21:02

6 Answers 6

319

Try this rule before your other rules:

RewriteRule ^(admin|user)($|/) - [L]

This will end the rewriting process.

Sign up to request clarification or add additional context in comments.

10 Comments

Hello Gumbo, your code worked if user type domain.com/admin/ (with ending slash) but not without it. Do you what can be changed for both cases to work? Thanks
@Kelvin: The alternation ($|/) should handle both cases.
Isn't it better to use RewriteCond here instead of RewriteRule?
@rineez RewriteCond does always require a RewriteRule that it is attached to. And adding this condition as a negated RewriteCond to every RewriteRule can be quite bulky, a single RewriteRule as an exit above those that should be ignored in such a case is more elegant.
@Ergec It means “don’t change anything”.
|
126

What you could also do is put a .htaccess file containing

RewriteEngine Off

In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.

3 Comments

+1 ~ .htaccess operates hierarchically, so local folders override their parents, just like a normal cascade in CSS or MVC.
there is only one thing you should have in mind: activating .htaccess files (setting AllowOverride to anything else than None in apache config) forces apache to search for .htaccess on every request. so if you have a high frequented website/webserver that filesystem/harddisk access could have an imense performance impact...
Perfect advice for validating renewal of SSL certificates via the file method. No need to modify anything outside the temporary directories, or in the vhost file. Once the validation is done, you can just delete the whole folder and you're done. Just great.
28

add a condition to check for the admin directory, something like:

RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]

RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]

4 Comments

Hello Rob, I put this line right after "RewriteBase /", and now I am getting "500 Error - Internal Server Error"
@Kelvin I edited my answer, add the condition before each rule you want it to apply to.
This one was the only one that actually helped me. The accepted answer did not help me.
The question mark is in !^/?(admin|user)/ not necessary if you know for sure the folders are in root, is it?
6

We used the following mod_rewrite rule:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

This redirects (permanently with a 301 redirect) all traffic to the site to http://www.newdomain.com, except requests to resources in the /test and /my-folder directories. We transfer the user to the exact resource they requested by using the (.*) capture group and then including $1 in the new URL. Mind the spaces.

Comments

6

If you want to remove a particular directory from the rule (meaning, you want to remove the directory foo) ,you can use :

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/foo/$
RewriteRule !index\.php$ /index.php [L]

The rewriteRule above will rewrite all requestes to /index.php excluding requests for /foo/ .

To exclude all existent directories, you will need to use the following condition above your rule:

RewriteCond %{REQUEST_FILENAME} !-d

the following rule

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php$ /index.php [L]

rewrites everything (except directories) to /index.php .

Comments

5
RewriteEngine On

RewriteRule ^(wordpress)($|/) - [L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.