regex - htaccess 文件重写 API 路由
问题描述
我正在尝试将所有传入流量重定向到我的站点到 index.php 并将 URL 的其余部分放在路径变量中。我的根目录中还有一个 index.html,如果用户直接访问它,我希望它们重新路由到 index.php 脚本。
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
重新路由似乎工作正常,但我所有的 /api/index.php?route=xx&mode=xx 似乎也正在重新路由。有没有办法从重写条件中排除 api 目录?
解决方案
请您尝试以下操作。这是检查是否REQUEST_URI
不是从那时开始/app/index.php
重新路由所有内容index.php
以及将参数传递给它,目前无法对其进行测试,将在几分钟左右进行测试。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]
推荐阅读
- c - 在c中引用双指针的属性
- python - 如何检查输入后是否使用了字母?
- php - 如何将安装在主机服务器上的 phpmyadmin 连接到正在运行的 docker 容器内的 maria db?
- scala - 如何将案例类作为变量传递给ScalaReflection
- mongodb - 在对话中获取 2 人之间的最新消息 Mongo
- firebase - Firebase 功能未运行/记录
- python - 不和谐.py; 警告/罢工系统
- algorithm - 算法的大 O 时间复杂度
- node.js - 如何在typeorm中找到(userId = a &&friendId = b)和(userId = b &&friendId = a)的数据?
- javascript - 如何使用纯 JavaScript 实现单击并按住绘图