Checking if file exist in other directory

Discuss practical ways rearrange URLs using mod_rewrite.

Checking if file exist in other directory

Postby dimitre » Sat Jan 17, 2009 9:14 am

I can verify if file exist in current directory, but now I need to check it in a subdirectory, from what I've read it would be something like this

Code: Select all
RewriteCond %{DOCUMENT_ROOT}/_cache%{REQUEST_URI}.html -f
RewriteRule ^(.+)$ _cache/$1.html [L]


But no success at all
The most similar code that works is this one

Code: Select all
RewriteCond %{DOCUMENT_ROOT}/_cache/rgb.html -f
RewriteRule ^(.+)$ _cache/rgb.html [L]

so probably I am having problems with %{REQUEST_URI} variable.
any ideas?
Thank you
Last edited by dimitre on Sun Jan 18, 2009 5:39 pm, edited 1 time in total.
dimitre
 
Posts: 4
Joined: Fri Jan 16, 2009 8:55 pm

Postby richardk » Sat Jan 17, 2009 6:21 pm

What URL are you visiting?
Where is the mod_rewrite?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/_cache/$1.html -f
RewriteRule ^(.+)$ /_cache/$1.html [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dimitre » Sat Jan 17, 2009 6:41 pm

I am visiting URLs like this:
http://project.com/rgb/
http://project.com/project2/

Mod_rewrite is on .htaccess on root folder of the site, not the server

and I can sucessifully check if there is a file called for example rgb.html, and redirect to it, in the case it is there.

but now I wanted to check it in a specific folder, called _cache, inside site root.
Thanks
dimitre
 
Posts: 4
Joined: Fri Jan 16, 2009 8:55 pm

Postby richardk » Sun Jan 18, 2009 11:19 am

If you go to /rgb/ then that is the value of %{REQUEST_URI} therefore the RewriteCond tests for .../_cache/rgb/.html not .../_cache/rgb.html.

Try the code from my last post, or if that doesn't work try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}/_cache%1.html -f
RewriteRule . /_cache%1.html [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dimitre » Sun Jan 18, 2009 5:38 pm

Thank you, works like a charm!

I have other tiny doubt, maybe easier
I am using the following lines to use wildcards pseudo subdomains, but it seems its not accepting numbers as part of the name.
I know its possible to condense 2 conditions in one line, but not enough REGEXP knowledge here

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www\.hifolio\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.hifolio\.com?$
RewriteRule ^([^/]*)/?([^/]*)?/?$ preview.php?user=%1&id=$1 [QSA,L]


Thank you
dimitre
 
Posts: 4
Joined: Fri Jan 16, 2009 8:55 pm

Postby richardk » Tue Jan 20, 2009 3:21 am

Which part (the sub domain or the directories) is not accepting numbers?
What happens when you enter a number?
It could be a problem with the sub domains' DNS (as the mod_rewrite looks like it should work).

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www\.hifolio\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.hifolio\.com$ [NC]
RewriteRule ^(?:([^/]+)(?:/[^/]+)?/?)?$ preview.php?user=%1&id=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dimitre » Tue Jan 20, 2009 6:25 am

the first part, like one123.hifolio.com.
Now I noticed I have a problem in somewhere else other than mod_rewrite because in apache logs I have a strange 404 request to subdomain.hifolio.com/home

Thanks for the reply
dimitre
 
Posts: 4
Joined: Fri Jan 16, 2009 8:55 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 27 guests

cron