Forum Moderators: phranque

Message Too Old, No Replies

Htaccess virtual directories advice

To see if the code is well written

         

orfaon

10:59 pm on Jun 11, 2010 (gmt 0)

10+ Year Member



Hello all...

i've found a lot of useful informations on this great website, and i 've come to develop my own htaccess file for my needs.

i still need to know if there is any flaws in it

MY NEEDS:
*** i want to be able to access this kind of url :
www.domain.com/docs/ => www.domain.com/index.php?pg=docs
www.domain.com/docs/123/ => www.domain.com/index.php?pg=docs&entity=123
www.domain.com/docs/123/edit/ => www.domain.com/index.php?pg=docs&entity=123&action=edit

*** i need to have hidden dirs and putting php files that can still be accessed in a regular way (no rewriting)
eg : www.domain.com/hiddendir/admin.php => www.domain.com/hiddendir/admin.php

*** the ability to add dynamic variables at the various depth (the [QSA] tag is here for that
www.domain.com/docs/?style=red => www.domain.com/index.php?pg=docs&style=red
www.domain.com/docs/123/?layout=horizontal&foo=bar => www.domain.com/index.php?pg=docs&layout=horizontal&foo=bar

HERE IS the code.
Thanks a lot in advance for any advice or thought about this, i'm not really a specialist, but this kind of work on my website, so maybe it's good...

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/ /index.php?pg=$1&entity=$2&action=$3 [QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/ /index.php?pg=$1&entity=$2 [QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/ /index.php?pg=$1 [QSA]

Thanks again for all the help !

g1smd

7:29 am on Jun 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the [L] flag to every rule.

Instead of [a-zA-Z], use [a-z] with the [NC] flag. It will parse faster.

Those -f and -d "exists" checks will slow your server to a crawl. Every page request, every image request, and every request for CSS and JS files will result in .htaccess checking your disk SIX times to see if there is a match.

Add an "exclusion" RewriteCond at the beginning of each ruleset to exclude any and all file extensions that will not ever be rewritten.

End-anchor your patterns, otherwise you are opening up your site to Duplicate Content issues.

If your URLs are extensionless, it is also likely that you can use much more specific patterns and do away with at least the -f checks, if not the -d checks as well.