Yasir Mushtaq
Yasir Mushtaq

Reputation: 178

How to check in .htaccess is the subdirectory

I have a .htaccess file, in which I rewrite rule

RewriteRule ^([0-9A-Za-z]+)$ profile.php?username=$1

It makes myproject.com/profile.php?username=iamuser tomyproject.com/iamuser

But what if I want to go to a folder, for example "images", when i write myproject.com/images, it remain on profile.php as 'images' as a username

Please help. Thanks

Upvotes: 0

Views: 65

Answers (1)

Ben
Ben

Reputation: 5129

You should use RewriteCond to check if the request URI belongs to any existing files / directories.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9A-Za-z]+)$ profile.php?username=$1 [QSA,L]

-d tests whether it exists and is a directory. -f tests whether it exists and is a file. The QSA flag appends the query string if any of the original request. The L flag makes it the last rule to process. No other rules behind are processed if this rule matches.

Upvotes: 1

Related Questions