如何签入.htaccess是子目录

时间:2018-02-01 14:17:33

标签: php apache .htaccess mod-rewrite

我有一个.htaccess文件,我在其中重写规则

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

使myproject.com/profile.php?username=iamuser成为myproject.com/iamuser

但是如果我想去一​​个文件夹,例如“images”,当我写myproject.com/images时,它会在profile.php上保留为'images'作为用户名

请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

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.