简体   繁体   中英

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 to myproject.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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM