Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Current I have an .htaccess file like this.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/(always|rewrite|these|dirs)/ [NC]

RewriteRule ^(.*)$ router.php [L,QSA]

It works create when the site files are in the document_root of the webserver (ie. domain.com/abc.php -> <DOCUMENT_ROOT>/abc.php). But in our current setup (which isn't changeable), this isn't ensured. We can sometimes have arbitrary folder in between the document root and folder of the .htaccess file (ie. domain.com/something/abc.php -> <DOCUMENT_ROOT>/something/abc.php). The only problem with is that is the second RewriteCond no longer works. Is there anyway to dynamically check if the accessed path by a path relative to .htaccess file.

For Example:

If I have a site where domain.com/prefix/ is the directory of the .htaccess file.

NOT FORCED TO REWRITE -> domain.com/prefix/index.php
FORCED TO REWRITE -> domain.com/prefix/rewrite/index.php

If I have a site where domain.com/ is the directory of the .htaccess file.

NOT FORCED TO REWRITE -> domain.com/index.php
FORCED TO REWRITE -> domain.com/rewrite/index.php
share|improve this question

3 Answers 3

up vote 1 down vote accepted

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)!)

share|improve this answer

If I do understand you correctly (particularly I don't know what you mean with forced to rewrite), RewriteBase is for you.

BTW: don't use stuff like domain.com :-)

share|improve this answer

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]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.