tags:

views:

99

answers:

2

I have a website, which has numerous subdirectories already. (All existing in server's filesystem) I want to create new "virtual" sub-dirs with htaccess, but I only want the htaccess rule work for directories, listed in DB, and not existing in filesystem.

i.e. File system has: /dir1/ & /dir2/

MySQL database has record for 'dir3' & 'dir4'

And I want:

A: mysite.com/dir1/ and mysite.com/dir2/ display existing old content

B: mysite.com/dir3/ and mysite.com/dir4/ display content from MySQL provided by PHP sctipt via redirect like: mysite.com/myscript.php?dir=dir3

C: mysite.com/dir5/ display 404 error (Dir does not exist in DB nor in Database)

Basically I want .htaccess to work like this: IF DIR Exists in DB - apply the rewrite rule and show content from myscript.php?dir=DIR

ELSE don't apply any rule.

I can create a separate php script, which can return 0/1 when given dir name exist in DB or not, but how do I make mod_rewrite get the data from that script?

Is it possible with htaccess/PHP at all?

A: 

You can't do a check like that in .htaccess afaik. I would try to hardcode the old content folders into the .htaccess and use a pattern to rewrite the rest.

Fabian
+1  A: 

Option 1: Make following htaccess:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ _redirect.php/$1 [L]

Which will redirect all dirs and files that don't exist physically to php script. Inside that script you can manually parse requested url and check your mysql table what to do next - forward or return 404.

Option 2: If your table with folders doesn't change often and/or you need speed, generate your htaccess file from php after table has changed, and map there all virtual folders from db.

serg
Thank You! Option #1 seems to be working!
Arseni