HTAccess Url重写规则

时间:2014-11-11 15:24:53

标签: php .htaccess mod-rewrite redirect

我试图仅使用API文件访问.htaccess,并且我遇到了问题。

例如,我想要实现的目标是:

  • 用户到达http://localhost/teammngr/user/photos/secrethash并自动重定向到http://localhost/teammngr/user/photos.php?sid=secrethash

  • 用户到达http://localhost/teammngr/user/config/secrethash并自动重定向到http://localhost/teammngr/user/config.php?sid=secrethash

  • 用户到达http://localhost/teammngr/team/members/secrethash并自动重定向到http://localhost/teammngr/team/members.php?sid=secrethash

如果用户想直接访问这些文件,则应该可以。 此外,网址http://localhost/teammngr/将位于http://team.mywebsite.com/等子域下。

到目前为止,我已经制作了以下.htaccess文件,但它一直在我的服务器上抛出 500错误。 需要明确的是,此文件不在根目录中,而是在子目录中。

Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr

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

RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$   /user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$   /team/$1.php?sid=$2 [L]

我在哪里弄错了?

感谢您的宝贵帮助。

2 个答案:

答案 0 :(得分:1)

RewriteCond仅适用于下一个RewriteRule。为避免在每个规则之前重复RewriteCond,您可以使用单独的规则来忽略对文件和目录的请求。同时从目标URI中删除/

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /teammngr/

# ignore requests for files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^user/(\w+)/([\w-]+)/?$ user/$1.php?sid=$2 [L,QSA]
RewriteRule ^team/(\w+)/([\w-]+)/?$ team/$1.php?sid=$2 [L,QSA]

答案 1 :(得分:0)

你可以试试这个:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr

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

RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$   user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$   team/$1.php?sid=$2 [L]