首页 > 解决方案 > 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 目录?

标签: regex.htaccessmod-rewrite

解决方案


请您尝试以下操作。这是检查是否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]

推荐阅读