How make rewrite rules relative to .htaccess file
Solution 1:
There's a solution but it's not simple.
Use RewriteMap
.
Here's how I would do:
Create a RewriteMap file. See here for more information. This is a very simple text file with: first, the wrong URL without the '/', then one space (at least) and then the right url, like this:
ok /folder1/
ok /folder2/
ok /folder3/
Then the code:
RewriteMap myfoldermap \
dbm:/web/htdocs/yourwebsite.com/rewriterules/myfoldermap.map
RewriteCond %{REQUEST_URI} ^/([^/]+)/(.*)
# Now in '%1' there's the name of the folder
# Don't touch the URL but...
# ...check if it exists in the map file
RewriteRule (.*) - [QSA,E=FOLDERMAP:${myfoldermap:%1|notfound}]
# if the environment is neither empty or not found...
RewriteCond %{E:FOLDERMAP} !^(notfound|)$
# this means we found something...
# => apply the RewriteRule
RewriteRule ^(.*)$ router.php [L,QSA]
If it doesn't work: read my usual "two hints", and add the rewrite log in your question.
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess
files), try to use the RewriteLog
directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
Solution 2:
Does Forced to rewrite means that the rewrite rule should work even if requested file or directory exists?
If yes, just try to avoid creating the file / directory.
Or seperate it to two rules:
RewriteRule ^/(always|rewrite|these|dirs)/ router.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php [L,QSA]