Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect all sub-directories to root using htaccess or httpd.conf?

I have an index file that builds content based on 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/

instead of:

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

So far I've tried a number of variations of:

RedirectMatch ^/.+/.*$ /

with no success.

I have 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]

Problems with this solution:

  • Inelegant and unscalable, requires manual line for each subdirectory
  • Fails with non alphanumeric characters (primarily +,= and &) ex. site.com/ab&c/de+f/ (note, even changing the regex to ^([a-zA-Z0-9_-\+\=\&]+)/?$ does little to help and actually makes it error out entirely)

Can you help?

like image 240
Ryan Avatar asked Jul 29 '11 19:07

Ryan


People also ask

How do I redirect a root to a subfolder?

You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload. Once connected, upload (or create) a text file named .

Does htaccess work on subdirectories?

htaccess file are applied to the directory in which the . htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been . htaccess files in directories higher up.


2 Answers

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 had [NC] flag ... so there were no need to have A-Z in your pattern.

2. Instead of [a-zA-Z0-9_-\+\=\&] or [a-zA-Z0-9_-] I use [^/] which means any character except slash /.

3. [QSA] flag was added to 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]

In reality, if you do not plan to show that URL anywhere (like, 301 redirect etc), the last line can easily be replaced by RewriteRule .* index.php [L] -- you will look for original URL using $_SERVER['REQUEST_URI'] in your PHP code anyway.

like image 112
LazyOne Avatar answered Nov 15 '22 09:11

LazyOne


Adding the following will redirect all traffic (for files that do not exist) to the index page:

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

Then, you can make your decision in index.php by parsing the 'REQUEST_URI'

like image 29
Scripty Avatar answered Nov 15 '22 07:11

Scripty