.htaccess in root and subfolder, each to redirect to own index.php

jezmck picture jezmck · May 23, 2011 · Viewed 19.8k times · Source

I apologise for a seemingly duplicate question, but none of the dozens I've looked at actually had the same problem.

I have the following directory structure:

/.htaccess
/index.php
/subfolder/.htaccess
/subfolder/index.php

I'd like all requests for pages to be handled by /index.php, unless the request starts /subfolder in which case it should be handled by /subfolder/index.php

  • e.g. /abc to be rewritten to /index.php?u=abc
  • e.g. /subfolder/def to be rewritten to /subfolder/index.php?u=def

I've been going round in circles over this, so any help will be massively appreciated.

EDIT: forgot to mention the problem! Requests within the subfolder are handled by the root index.php, not the subfolder one. (Except requests for /subfolder)

Current File contents

/.htaccess
Options -Indexes -MultiViews +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
/subfolder/.htaccess
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]

Answer

anubhava picture anubhava · May 23, 2011

Have your root .htaccess like this:

Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin/)(.+)$ /index.php?u=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.+)$ /admin/index.php?u=$1 [NC,QSA,L]

There is no need to have .htaccess in admin folder for this simple requirement.