0

I am trying to configure an .htaccess file to reroute all urls to the nearest index subdirectory, like this:

http://example.com/admin/blah/test/whatever should stay in the address bar, but point to:

If the url is that of an actual file, it should always go to that file. so http://example.com/admin/blah/file.css or http://example.com/admin/blah/item.inc should all still work if they exist, otherwise it should redirect to index.php in that folder, or nearest parent folder like shown above.

I'd also like to have this only affect a subfolder, if that's at all possible. In the above example, I'm assuming the .htaccess file would go in the /admin/ folder.

Update: Here's what currently works:

# No Directory Listings or trailing slashes Options -Multiviews -Indexes +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On DirectorySlash Off # See if the current request is a directory containing an index file RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^(.*)/?$ $1/index.php [L,QSA] # if index.php doesn't exist in current dir then # forward to the parent directory of current REQUEST_URI RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA] </IfModule> 

This mostly works. If I type in http://example.com/admin?p=2 it resolves as expected, but if I use the URL http://example.com/admin/?p=2 it ALSO resolves, without explicitly removing the trailing slash.

1
  • If you want subfolder-specific rules, put the .htaccess file in the subfolder. Commented Jan 15, 2014 at 20:42

2 Answers 2

1

This is fairly tricky rule. Try this in your your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On RewriteBase / # if index.php doesn't exist in current dir then # forward to the parent directory of current REQUEST_URI RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L] 
Sign up to request clarification or add additional context in comments.

3 Comments

This seems to work well. One thing I've noticed is on my local testing server, I have multiple sites in subfolders in the root directory, so I had to hardcode RewriteBase to /sitefolder/ which isn't a problem in a testing environment. What WAS interesting, though: it worked great with the example url and localhost/sitefolder/subfolder/index.php went to the correct file but localhost/sitefolder/subfolder/index.php/ went to localhost/ Might just be a testing server quirk. I'll do some more tests and report back in a minute.
Thank you so much for your help. It works all but one small issues: I added [L,QSA] to save any query strings, which works great, but, is there any way to drop the trailing slash? Currently, the url is this: example.com/admin/?p=3 and I'd like this example.com/admin?p=3 which if I try to use, it adds the slash back in.
This rule doesn't add trailing slash. Also if /admin/ is a directory then trailing slash is added by mod_dir module.
0

Here's what eventually worked.

In my .htaccess file I had:

# No Directory Listings Options -Multiviews -Indexes +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On DirectorySlash Off # see if the current directory contains an index file RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^(.*)/?$ $1/index.php [L,QSA] # if index.php doesn't exist in current dir then # forward to the parent directory of current REQUEST_URI RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA] </IfModule> 

Links to existing directories look for an index.php in that directory. Links to existing files display that file. Everything else redirects to my index.php. From there, I can read any queries with $_GET as normal and I can read and parse the full URL to see what page to display. This also works in a subdirectory if I don't want it to apply to an entire site.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.