[英]HTAccess Url rewriting rules
我试图使用.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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.