I've a catalog file that develops content according to n PATH_INFO variables.

Example:

site.com/A/B/n/

should use index.php at either:

site.com/index.php?var1=A&var2=B&varN=n
 - or - 
site.com/index.php/A/B/n/

rather than:

site.com/A/B/n/index.php || which doesn't exist ||

To date I have attempted numerous versions of:

RedirectMatch ^/.+/.*$ /

without results.

I've an inelegant and unscalable solution here:

RewriteEngine On 
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]

Issues with this solution:

  • Inelegant and unscalable, requires manual line for every subdirectory
  • Fails with non alphanumeric figures (mainly +,= and &lifier) ex. site.com/ab&c/p+f/ (note, even altering the regex to ^([a-zA-Z0-9_-+=&lifier]+)/?$ does nothing to assist and really causes it to be error out entirely)

Are you able to help?

Option 1: (site.com/index.php?var1=A&var2=B&varN=n):

Options +FollowSymLinks -MultiViews
RewriteEngine On

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteRule ^([^/]+)/?$ index.php?p1=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]

1. You'd [NC] flag ... so there have been you don't need to have A-Z inside your pattern.

2. Rather than [a-zA-Z0-9_-\+\=\&] or [a-zA-Z0-9_-] I personally use [^/] meaning any character except slash /.

3. [QSA] flag was put into preserve existing query string.

Option 2: (site.com/index.php/A/B/n/):

Options +FollowSymLinks -MultiViews
RewriteEngine On

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

The truth is, if you don't intend to reveal that URL anywhere (like, 301 redirect etc), the final line may be easily changed by RewriteRule .* index.php [L] -- you'll search for original URL using $_SERVER['REQUEST_URI'] inside your PHP code anyway.

Adding the next will redirect all traffic (for files that don't exist) towards the index page:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]

Then, you may make your choice in index.php by parsing the 'REQUEST_URI'