php - 如何在 htaccess 中设置 mod_rewrite?

标签 php .htaccess

如何设置 mod_rewrite?我的目录结构如下(www.example.com/user/account/)

index.php
.htaccess

.htaccess

DirectorySlash Off
RewriteEngine on
RewriteBase /user/account/
RewriteRule ^([\w-]+)/?$ index.php?command=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]

我可以访问www.example.com/user/account/setting,其中command =setting

但我无法访问 www.example.com/user/account/setting/ 其中 command =setting

当我改变

 RewriteRule ^([\w-]+)/?$ index.php?command=$1&page=$2 [L,QSA]

我无法访问www.example.com/user/account/setting/1,其中command =setting & page = 1

请帮助我如何访问以下链接?

www.example.com/user/account/setting/1

最佳答案

您需要一个新的规则来处理 page 参数,方法是从漂亮的 URI 中捕获它:

DirectorySlash Off
RewriteEngine on
RewriteBase /user/account/

RewriteRule ^([\w-]+)/?$ index.php?command=$1 [L,QSA]

RewriteRule ^([\w-]+)/(\d+)/?$ index.php?command=$1&page=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]

关于php - 如何在 htaccess 中设置 mod_rewrite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395806/

相关文章:

php - 找不到行的值

php - 从 xpath 查询中排除节点

PHP递归文件文件夹扫描按修改日期排序

php - 301 重定向但排除子目录

.htaccess - 如何设置 301 重定向到新域

php - 如何在 slider 中显示视频

XAMPP Windows 上的 Php Cron 作业

PHP 警告 : No such file or directory in Unknown on line 0

ajax - 将 AJAX 与 .htaccess 重定向的子域一起使用

php - mod_rewrite 与影响其他规则的先前规则