Reputation: 19847
I'm wondering how I can rewrite first level sub folders which do not have a trailing slash to a php file using mod rewrite.
For example
/test -> rewrites to file.php?id=test
/test/ -> trailing slash so it follows through as normal to /test/
/test/test -> second level sub folder so it follows through as normal to /test/test
/ -> homepage needs to follow through as usual to index.php
Was trying to think of a rule to do this in 1 line. For the above example, test is just an example and the rule should rewrite any string.
Upvotes: 1
Views: 216
Reputation: 785611
You can use this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]+)$ file.php?id=$1 [L,QSA]
Upvotes: 1
Reputation: 143906
You can only do this by disabling mod_dir's DirectorySlash
. There's very good reasons for requiring trailing slashes on directories because otherwise the entire contents can be viewable even if there's an index file. So this is incredible risky.
DirectorySlash Off
RewriteEngine On
# attempt to add trailing slashes
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)/(.+[^/])$ /$1/$2/ [L,R]
# route to file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /file.php?id=$1 [L]
Upvotes: 1