Drewdavid
Drewdavid

Reputation: 3192

.htaccess rewrite only files in a certain directory

I found this question which is similar to my request here, but I'm not sure how to modify it to my need

I want to ...

a) Drop the directory (edited, forgot to mention)

b) Drop the file extension

c) Replace it with a trailing slash

... on files contained only within the /categories/ directory

So the effect is that website.com/categories/My-File-Name.php becomes website.com/My-File-Name/

This is what I've tried so far:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/categories/$
RewriteRule ^([^\.]+)/$ /categories/$1.php [NC,L]

Upvotes: 2

Views: 2144

Answers (1)

anubhava
anubhava

Reputation: 785146

This should work:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/categories/$1.php -f
RewriteRule ^(.+?)/?$ /categories/$1.php [L]

Upvotes: 1

Related Questions