Php HTAccess Url重写规则
我试图用Php HTAccess Url重写规则,php,.htaccess,mod-rewrite,redirect,Php,.htaccess,Mod Rewrite,Redirect,我试图用.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/us
.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
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
。为了避免在每个规则之前重复重写cond,您可以使用单独的规则忽略对文件和目录的请求。还要从目标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]
为什么要重写库/teammngr。你的URL中没有
teammngr
。@anubhava你赢了我哈哈:)对不起,我在psot中犯了一个错误。让我编辑一下。Updated@Manitoba您是否已确保启用了mod_rewrite
?尝试删除你的第二个重写规则
,看看你是否仍然有错误?仍然有问题。+1我能想到的唯一其他问题是,如果他的案例没有启用mod_rewrite,除此之外,你的答案会特别通过包含-MultiViews
来清除它!感谢@Prix,我添加了-MultiViews
,因为PHP文件名类似于URI路径,但您是对的,500的原因也可能是mod_rewrite
未启用。谢谢您的回答。我仍然收到错误,但是我的服务器上启用了mod_rewrite。好的,检查您的Apache error.log,看看这个问题的确切错误行。显然,Options+FollowSymlinks
不应该出现在.htaccess
中,因为它已经在Apache conf
中启用。