对.htaccess规则应该如何工作的方式感到困惑

时间:2011-03-12 08:18:20

标签: php .htaccess

我的.htaccess

中有这个
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]

在我的php中我有

header("Location: /users/" . $_SESSION['user']);

将其加载到浏览器中

foo.bar/users/s2xi

这给了我一个404 Not Found错误,我可以理解目录用户不存在的部分,但不是我的RewriteRule应该照顾它吗?

编辑:我的完整.htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#RewriteRule ^([a-zA-Z0-9_\s-]+)/([a-zA-Z0-9_\s-]+)(/)?$ users.php?type=$1&user=$2 [NC,R]
#RewriteRule ^users/?$ users.php [L]
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
#RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]
#RewriteRule ^(admin)/(.+)/?$ users.php?user=$1 [NC,L]
RewriteRule ^admin/([^/]+)$ users.php?user=$1 [NC,L]

#error codes
RewriteRule ^/error/([^/]+)/ error.php?code=$1 [NC]

1 个答案:

答案 0 :(得分:1)

将.htaccess更改为以下内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^users/([^/]+)$ users.php?user=$1 [NC,L]

还要确保users.php位于正确的目录中。如果它在根目录中,您可以使用:

RewriteRule ^users/([^/]+)$ /users.php?user=$1 [NC,L]