Exclude directories from URL rewriting
Posted in SEO on December 3rd, 2009 by Ilir Fekaj – Be the first to commentRewriting your URLs as directories in .htaccess is a really cool feature which greatly improves usability and search engine visibility of your pages. However, this can make real directories inaccessible and you may wish to exclude these from being rewritten. For example, you might want to exclude your temp, stats or admin directory. The simplest way to do this would be to place the following code in your .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
The first line of code checks if the directory requested by user exists on the server file system, and if it does exist, it doesn’t serve index.php. Second line does the same thing for files. In case request is not made for any files or directories that exist, it performs rewrite on index.php.
This works in many cases, but if you have more complex rewrites performed on several files, I found that this code does better job:
RewriteRule ^(temp|stats|admin)(/.*)?$ - [L]
Put all directories you want to exclude from rewrite in brackets and separate them with pipe symbol “|”.