为 php switch 语句重写 url

Rewrite urls for a php switch statement

我正在尝试使用 .htaccess 来创建友好的 url 的不同组合,但是 none 正在工作。

我有一个名为 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');

}

并根据操作参数将您发送到名为用户的文件夹和添加或编辑页面,因此 url 如下所示:

Url 1: http://project.blu/users?action=edit

Url 2 (with id parameter): http://project.blu/users?action=edit&id=1

我想实现这些期望 urls:

Url 1: http://project.blu/users/edit

Url 2 (with id parameter): http://project.blu/users/edit/1

我已经尝试过这段代码,但它没有成功,我假设是因为 switch 语句需要查看操作参数才能知道要显示哪个页面:

Url 1 个尝试过的解决方案:

RewriteRule ^users/([-\w]+)$ users.php?action= [NC,L]

Url 2 个尝试的解决方案:

RewriteRule ^users/([-\w]+)$/(\d+)$ users.php?action=&id= [NC,L]

两者都不适合我。

注意:我还有其他规则可以从 url 中删除 .php 并且这些规则工作正常所以例如这个 url http://project.blu/users 我可以查看页面没问题。

这是我的完整 .htaccess 文件:

Options +FollowSymLinks -MultiViews
RewriteEngine on

# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^Pages/(.+)$ / [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 ^(.*)$ .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= [NC,L,QSA]

RewriteRule ^users/([\w-]+)/(\d+)/?$ users.php?action=&id= [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/[=15=] [L]

这是我的文件夹结构的图像:

站点根目录.htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on

# send all the traffic to /src/
RewriteRule .* src/[=10=] [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/.php?action= [NC,L,QSA]

RewriteRule ^(users|locations)/([\w-]+)/(\d+)/?$ Pages/.php?action=&id= [NC,L,QSA]

# check & rewrite if a matching .php file exists in Pages folder
RewriteCond %{DOCUMENT_ROOT}/src/Pages/.php -f
RewriteRule ^(.+?)/?$ Pages/.php [L]

禁用 MultiViews 或 Apache 的内容协商服务很重要。

选项 MultiViews(参见 http://httpd.apache.org/docs/2.4/content-negotiation.html)由 Apache's content negotiation module 使用,它在 之前 mod_rewrite 运行并使 Apache 服务器匹配文件的扩展名。因此,如果 /file 是 URL,那么 Apache 将提供 /file.html.