This question is difficult, but let me try. I am going to change the link structure on my website and was hoping to have my index file basically handle most of what needs to be done in order of discovering what page needs to be opened (or included) based on the URL supplied. For now I’m leaving my original rewrites with updated canonicals to point search engines to the new pages. So, to keep rewrites, while allowing the index.php to pick up everything else, would this work?
RewriteRule ^/?trailer/([-0-9a-zA-Z#]+)/([-0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?$ file1.php?fkey=$1&tkey=$2&tres=$3
RewriteRule ^/?trailers/([0-9]+)$ file2.php?page=$1
RewriteRule ^/?communities social.php
RewriteRule ^/?user/([0-9]+)?/?([0-9a-zA-Z#]+)?/?([0-9]+)? usr.php?uid=$1&pagetype=$2&page_number=$3
//and so on and so on of rewrite rules... and then:
RewriteRule ^/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)? index.php?var1=$1&var2=$2&var3=$3&var4=$4
So basically if the URL doesn’t fit any of the exact rewrites, then it checks it against the final rewrite url that has the index.php give it a look.
You use a LOT of optional atoms so your scripts do need to handle null values.
EVERYTHING in the final RewriteRule is optional so it will match everything (without dot characters). This SHOULD be okay given your presented code BUT, to be safe, I’d preface that with a RewriteCond {REQUEST_FILENAME} !-f but DEFINITELY check for directory, too (!-f). What you are doing is what WordPress has done for years.
For what it’s worth, when Apache 2 came out, it refused to match leading /'s while Apache 1 still required the / in the DocumentRoot. You’re using the OPTIONAL leading / (acceptable to both) but I doubt that there are many Apache 1 servers left out there so I’d just (be lazy and) delete the /? in your regex throughout.
To be honest, I’m using nginx, so I was using the apache rewrites to simplify what I’ve been doing (before converting to nginx standard) and what I wanted to change. My assumption that the conversion to nginx format would work just the same.
It’s probably just as important to check the directory, too.
Sorry, I can’t comment on your translation to nginx except to wonder … where their documentation is (NOT that I want to go look at it). I know that M$ has two versions of their attempt to emulate Apache’s mod_rewrite (and only one uses similar syntax) without duplicating all mod_rewrite’s features.
It’s definitely tricky, but looks like nginx combines the !-f and !-d into one, as demonstrated by WordPress and Drupal in the nginx settings. For example, this is the nginx conversion for WordPress: