Check if file exists in root called from url /dir/file.ext

New to mod_rewrite? This is a good place to start.

Check if file exists in root called from url /dir/file.ext

Postby elgus » Wed Aug 05, 2009 1:23 am

Hello,

I have following situation:

web is divided into sections
www.example.com/sectionA
www.example.com/sectionB
www.example.com/sectionC
etc

www.example.com/sectionA/document is redirected to www.example.com/index.php (with REQUEST['URI'] applied in php in order to get the name of the document and section)

now I also have
www.example.com/sectionA/fileA.php The fileA.php is located physically in web's root

but also have
www.example.com/sectionD/fileB.php The fileB.php is located physically in the physical directory sectionD

Now I'd like to:
from given url www.example.com/sectionA/fileA.php check if fileA.php exists in directory sectionA and call it
if not, check if fileA.php exists in root and call /fileA.php?section=sectionA
if not, call index.php

Is it possible? I am not able to read single filename from REQUEST. I was trying all possible and impossible with no success.
[EDIT] I have worked following code which seems to work, but... there might be a much better workaround.

Code: Select all
RewriteRule ^([^/!\.]+)/+([^/]+\.php)?$ - [E=file:$2,E=dir:$1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:dir}/%{ENV:file} !-f
RewriteCond /%{ENV:file} !-f
RewriteRule ^([^/!\.]+)/+([^/]+\.php)?$ $2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule . /index.php [L]




PS: Options +FollowSymLinks causes internal error 500.

Any help appreciated!
elgus
 
Posts: 3
Joined: Wed Aug 05, 2009 1:04 am

Postby richardk » Wed Aug 05, 2009 3:39 pm

Try
Code: Select all
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$2?section=$1 [QSA,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(?:/[^/]+)?/?$ /index.php?section=$1&uri=/$0 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby elgus » Thu Aug 06, 2009 11:58 pm

Thank you richardk !!
That works better than my code.

How does

Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$2 -f


work? I can't find info about it (how it knows $2)

E.
elgus
 
Posts: 3
Joined: Wed Aug 05, 2009 1:04 am

Postby elgus » Fri Aug 07, 2009 3:02 am

On my virtual server it works, howeveron my hosting '?:' in the last rule causes Internal Server Error.
elgus
 
Posts: 3
Joined: Wed Aug 05, 2009 1:04 am

Postby richardk » Fri Aug 07, 2009 1:44 pm

How does
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$2 -f

work? I can't find info about it (how it knows $2)

$2 is from the RewriteRule. The RewriteRule regular expression is tested first and then the RewriteConds. The RewriteRule substitution is done last.

On my virtual server it works, howeveron my hosting '?:' in the last rule causes Internal Server Error.

Then you can use (it didn't need to be like that anyway)
Code: Select all
^([^/]+)(/[^/]+)?/?$ /index.php?section=$1&uri=/$0

("?:" just means a backreference/variable is not created by the "()").
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 35 guests

cron