9

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
jezmck
  • 1,138
  • 3
  • 18
  • 38

2 Answers2

12

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • @anubhava this does not work when WordPress is the main site and CodeIgniter is in `subfolder `, what could be the reason. see here https://stackoverflow.com/questions/47467868/codeigniter-app-in-wordpress-subdomain-sub-folder – Smith Nov 26 '17 at 08:24
  • Most of the `mod_rewrite` rules don't work with PHP frameworks with a front controller architecture like WP, Joomla, CI etc. Better to use framework provided APIs for rewriting stuff instead of `mod_rewrite`. – anubhava Nov 26 '17 at 08:26
2

This line of the root folder .htaccess:

RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]

is causing all the requests to non-existent filepaths to be redirected to the root folder's index.php. That's the problem. One possible solution could be to substitute the above line with this couple of lines:

RewriteRule ^subfolder/(.*)$ /subfolder/index.php?u=$1 [L,NC,QSA]
RewriteRule ^(.+)$ /index.php?u=$1 [L,NC,QSA]

By adding the L (last) flag and writing the rules in this order you'll get Apache to redirect correctly your requests, and eliminate the need for rewriting directives into /subfolder/.htaccess .

Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
  • I tried similar at one point. Wouldn't that [L] make it a redirect? – jezmck May 23 '11 at 14:44
  • 1
    @jezmck No, that is done by the [R] flag. [L] causes the rewriting to be stopped from further rules application. – Paolo Stefan May 23 '11 at 15:24
  • http://stackoverflow.com/questions/3482106/rewrite-problem-last-not-being-respected/3482267#3482267 explains what I meant by that. – jezmck May 25 '11 at 08:52