apache - Slim 3 - 使用重写规则从 url 中删除 subdir

标签 apache .htaccess url-rewriting slim-3

我在 A2Hosting 上有一个共享主机,我最近从 public_html/ 移动了我的主域|至 public_html/subdir/
这是结构:

/public_html
 /subdir(site files of main domain)
  /api
  index.php  

我目前的 htaccess(public_html) 是:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
RewriteRule ^(.*)$ /subdir/$1 [L]

当我之前调用我的 API 时:domain.com/api/
但现在是:domain.com/subdir/api/
我的 htaccess 在 api是 :
RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

如何删除 url 中的“subdir”以保持我的 api 像以前一样?但是仍然使用我当前的 htaccess 将根指向我的子目录?

谢谢

最佳答案

一种方法是重写 /api从根目录中的主 .htaccess 文件中,如下所示:

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)$ ../subdir/api/$1 [R,L,QSA]

新/subdir/api 文件夹中的 .htaccess 文件可以保持原样。只需要调整 RewriteBase。

另一种方式是从之前的/api重写文件夹,如果您出于某种原因保留它:
RewriteEngine On
RewriteBase /api
RewriteRule ^(.*)$ ../subdir/api/$1 [L]

关于apache - Slim 3 - 使用重写规则从 url 中删除 subdir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50593569/

相关文章:

php - PDO、$_GET 和从 MySQL 数据库中进行选择

css - 错误文档无样式(服务器)

php - 如何在Windows操作系统上安装的wamp服务器上化妆用户名:password pair in . htpasswd文件

javascript - 使用javascript根据查询字符串替换链接的自定义ID

java - 如何在 Windows 7 上安装 Apache Ant

java - 写入 Excel 文件时出现问题 (Apache POI)

python - 与 mod_wsgi 相比, mod_python "work"怎么样?

php - 使用全局变量动态更改 URL

IIS URL 重写

php - 特殊字符和 URL 重写