我试图用.htaccess
文件简单地访问我的API
,但我遇到了一个问题。
例如,我想要实现的是:
-
用户到达
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
。
如果用户想直接访问这些文件,那么这是可能的。此外,url 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]
我在哪里犯了错误?
谢谢你的宝贵帮助。
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]
你可以试试这个:
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]