我正在尝试使用 .htaccess 的不同组合来创建友好的 URL,但没有一个有效。
我有一个名为 users.php 的 php 文件,如下所示:
<?php
use A\B\App\Url;
require_once("../../vendor/autoload.php");
$action = Url::getParam('action');
switch($action) {
case "add":
require_once('user/add.php');
break;
case "edit":
require_once('user/edit.php');
break;
case "delete":
require_once('user/delete.php');
break;
default:
require_once('user/list.php');
}
并根据操作参数,它会将您发送到一个名为 user 的文件夹和一个类似“添加”或“编辑”的页面,因此网址如下所示:
Url 1: http://project.blu/users?action=edit
Url 2 (with id parameter): http://project.blu/users?action=edit&id=1
我想实现这些所需的网址:
Url 1: http://project.blu/users/edit
Url 2 (with id parameter): http://project.blu/users/edit/1
我已经尝试过这段代码,但它不起作用,我假设是因为 switch 语句需要查看操作参数才能知道要显示哪个页面:
网址 1 尝试的解决方案:
RewriteRule ^users/([-\w]+)$ users.php?action=$1 [NC,L]
网址2尝试的解决方案:
RewriteRule ^users/([-\w]+)$/(\d+)$ users.php?action=$1&id=$2 [NC,L]
两者都不适合我。
注意:我还有其他规则从网址中删除 .php 并且这些规则工作正常,例如这个网址 http://project.blu/users
我可以查看页面问题。
这是我的完整 .htaccess 文件:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^Pages/(.+)$ /$1 [L,R=301,NC]
# landing page for app subdomain to show index.php
RewriteRule ^$ Pages/index.php [L]
# check & rewrite if a matching .php file exists in pages/ subdirectory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^users/([\w-]+)/?$ users.php?action=$1 [NC,L,QSA]
RewriteRule ^users/([\w-]+)/(\d+)/?$ users.php?action=$1&id=$2 [NC,L,QSA]
# Redirecting to the error page when nothing is found
ErrorDocument 404 /error
我的文件夹根目录中还有另一个 .htaccess,其中包含以下内容:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /Pages/
RewriteRule .* src/Pages/$0 [L]
这是我的文件夹结构的图像:
最佳答案
站点根目录.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /src/
RewriteRule .* src/$0 [L]
您可以在 src/.htaccess
中使用这些规则:
Options -MultiViews
RewriteEngine On
# landing page for app subdomain to show index.php
RewriteRule ^$ Pages/index.php [L]
# skip all files and directories from rewrite rules below
RewriteCond %{ENV:REDIRECT_STATUS} 200 [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# route dummy URLs to their php files inside Pages/ folder
RewriteRule ^(users|locations)/([\w-]+)/?$ Pages/$1.php?action=$2 [NC,L,QSA]
RewriteRule ^(users|locations)/([\w-]+)/(\d+)/?$ Pages/$1.php?action=$2&id=$3 [NC,L,QSA]
# check & rewrite if a matching .php file exists in Pages folder
RewriteCond %{DOCUMENT_ROOT}/src/Pages/$1.php -f
RewriteRule ^(.+?)/?$ Pages/$1.php [L]
禁用 MultiViews
或 Apache 的内容协商服务非常重要。
选项MultiViews
(参见http://httpd.apache.org/docs/2.4/content-negotiation.html)由之前mod_rewrite
运行的Apache内容协商模块
使用并使 Apache 服务器匹配文件的扩展名。因此,如果 /file
是 URL,那么 Apache 将提供 /file.html
。
关于php - 重写 php switch 语句的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68778479/