3

I need to create a following rule to place in my .htaccess

  1. For each request i'd like to execute file/path in subfolder subdir.
  2. 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

Community
  • 1
  • 1
Sebastian Dusza
  • 2,470
  • 2
  • 30
  • 54

3 Answers3

3

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.

Haymo Kutschbach
  • 3,322
  • 1
  • 17
  • 25
  • Because i need to deploy Zend Framework based app in an environment where root http public folder is the root of the whole virtual file system. And I don't want to rewrite the whole application. – Sebastian Dusza Feb 17 '12 at 14:41
  • so the url mapping should only apply, if matching your http://domain/folder/Subfolder/xxx.html scheme? So better use a RewriteRule than? – Haymo Kutschbach Feb 17 '12 at 14:43
  • why would you need to rewrite the application? – Haymo Kutschbach Feb 17 '12 at 14:49
  • mapping should apply to every request. If file/path is found under subdir folder then this file should be returned otherwise request should be redirected to index.php. – Sebastian Dusza Feb 17 '12 at 14:55
  • I use a lot of paths to files located inside public folder of ZF. I don't want to update those paths each time i need to adapt my app to new server environment. It is possible, I almost got the solution with my previous question. – Sebastian Dusza Feb 17 '12 at 14:57
3

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]  
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
2

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

Community
  • 1
  • 1
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81