I need to create a following rule to place in my .htaccess
- For each request i'd like to execute file/path in subfolder subdir.
- If the file doesn't exist there then i'd like to forward this request to index.php
Let say that .htaccess iw placed at http://domain/folder
When the user opens url http://domain/folder/Subfolder/xxx.html
he should recieve file from http://domain/folder/subdir/Subfolder/xxx.html
So these rules have to consider the fact that there are subfolders inside subdir.
(Different structures of subfolders :-)
And only if the path under subdir doesn't exist the request should be forwarded to index.php
Plase help :)
ps This is a followup question to my previous one. It is quite different and includes the fact that there are subfolders inside subdir.
-> mod_rewrite: How to search in local folder, subfolder and then redirect to index.php
This should do it:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ([\w]+\.[\w]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -f
RewriteRule ^ /%1 [NC,L]
RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f
RewriteRule ^ /subdir%{REQUEST_URI} [L]
RewriteRule ^ /index.php [L]
An update with a slight optimization.
RewriteEngine on
#RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^[^/]+\.[^/]+$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} (.*/)([\w]+\.[\w]+)$ [NC]
RewriteCond %1subdir/%2 -f
RewriteRule ^ subdir/%2 [L]
RewriteRule ^ index.php [L]
- The above will work with anydirectory/subdir
Change log:
RewriteBase commented for use of relative path.
Checking for /file.ext (or whether in current directory).
the below will check whether file is present in current directory.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^[^/]+\.[^/]+$ - [NC,L]
RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f
captures current directory in %1 and the file.ext in %2
Why not simply use an Alias:
Alias /folder /subdir
And redirect to index.php in your 404 page? It has the advantage, 404 is still caught into the weblog.
Try adding the following to the .htaccess
file in the root/folder
directory of your site.
RewriteEngine on
RewriteBase /folder
#capture the Subfolder/file.xx
RewriteCond %{REQUEST_URI} ^/[^/]+/([^/]+)$
#if the subdir/subfolder/file.xx exists
RewriteCond %{DOCUMENT_ROOT}/subdir/%1 -f
#Serve this file
RewriteRule ^ /subdir/%1 [L,S=1]
#else send to index.php in root directory
RewriteRule ^ /index.php [L]
I am assuming you want to send the request to the index.php in the root dir. If it is in the folder directory instead, change the last rule to
RewriteRule ^ index.php [L]