This is my .htaccess It feels like its way to long and that it can be made more efficient. The problems I’m having at the moment are that because of the order these rules are in the Rewrites for profile view and vcard view aren’t working at the moment. I figured out that depending in what order I place these RewriteRules it will affect how the others behave. Its kinda of a nightmare if you ask me.
My site structure isn’t so complicated. I have 3 PHP pages each page handles different actions of my project using the $_GET values for checking that the person is in the right place with the right parameters. One thing I was having issues with was making friendly links so people can go to an image or something using familiar url paths.
Well thats it, oh ya. Also I have a dynamic RewriteRule template for when I want to use an option or a setting, then I can use the last 2 place holders to put in my data.
# Options:
# -MultiViews: Turns off multiviews so it doesn't interfer with our rewrite rules
# -Indexes: Stop directory listings
# +FollowSymlinks: Let out rewrite rules work
Options -MultiViews -Indexes +FollowSymlinks
RewriteEngine On
RewriteBase /
# Error page
# Force add trainling slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://foo.bar/$1/ [R=301,L]
# Prepent www if not exist in URI
RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]
# If path is not a directory or file then apply RewriteRule
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]
# Rewrite for page view
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3 [QSA,L]
# Condition to ignore regular links
RewriteRule ^library/templates/([^/]+)/([^/]+)/([^/]+)/?$ library/templates/$1/$2/$3 [QSA,L]
# Rewrite for dynamic page settings
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3&$4=$5 [QSA,L]
# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [QSA,L]
# Rewrite for site default resources location
RewriteRule ^([^/]+)/([^/]+)/theme/s/([^/]+)/([^/]+)/([^/]+)/?$ library/themes/site/$3/$4/$5 [QSA,L]
# Rewrite for profile view
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
# Rewrite for vCard view
RewriteRule ^([^/]+)/vcard/?$ vcard.php?user=$1 [NC,L]
# Rewrite for user themes location
#RewriteRule ^([^/]+)/([^/]+)/theme/([^/]+)?$ library/themes/users/$3 [NC,L]
#error codes
#RewriteRule ^error/([^/]+)/?$ error.php?code=$1 [NC,L]
Since you have only one rule for each result URL, I don’t think you can simplify this — or not significantly — without altering your app’s URL structure. Your regexps are simple and, as far as I can tell, solid enough.
But yes, the structure is fragile and order-dependent:
view or to vcard view. Which requires
putting the account view rule after
the vCard view rule AND avoiding ever
having a user called “vcard”.
links or dynamic page settings — so
avoid swapping these two rules or having a type “library” and a
user “templates”.
I wouldn’t worry much about efficiency/performance: in one of my projects we’re using a similar setup with 308 RewriteRule and 307 RewriteCond (just counted them). Having the same worry you have, I once measured its performance by running a load test with random URLs vs. the transformed ones and the difference in performance was so small that it was simply not measurable against the backdrop of PHP & database processing.
Note: the fact that I have experience with something similar (strikingly similar I have to say) should not be construed to mean that I back it. URL structures which can’t be documented and explained easily are a nightmare and should be avoided if possible.