带有htaccess的子目录中的新重写规则 [英] New rewrite rules in subdirectory with htaccess

查看:101
本文介绍了带有htaccess的子目录中的新重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在.htaccess文件中配置重写规则. 我想在我的Wordpress文件夹中运行一个 Slim PHP App ,因此我可以为我的API重用SSL证书. Slim应用程序在/api文件夹中运行,因此可以通过调用https://www.example.com/api进行访问.

I'm struggling with configuring the rewrite rules in the .htaccess files. I'd like to run a Slim PHP App inside my Wordpress folder, so I can reuse the SSL certificate for my API. The Slim App is running inside the /api folder, so it can be accessed by calling https://www.example.com/api.

这很好用,但是如果我打开 example.com/api/timesheet 之类的API端点,则该页面将重定向到 404错误 Wordpress页面.

This works well, but if I open an API endpoint like example.com/api/timesheet the page is redirected to the 404 error Wordpress page.

到目前为止我做了什么:
我在Wordpress根目录下的.htaccess文件中排除了api directory:

What I did so far:
I excluded the api directory in the .htaccess file inside the Wordpress root directory:

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(api|api/.*)$
RewriteCond %{HTTPS} !on           
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

并在api directory内的.htaccess文件中添加了这些新的Slim默认规则:

And added these new Slim default rules in the .htaccess file inside the api directory:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

推荐答案

在经过多次尝试和错误RewriteBase /api之后,只需将其添加到api目录中的.htaccess文件中,即可使其正常工作.

Just added after a lot of try and error RewriteBase /api to the .htaccess file in the api directory to get it working properly.

/api中的最终.htaccess现在如下所示:

The final .htaccess in /api now looks like this:

RewriteEngine On
RewriteBase   /api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

这篇关于带有htaccess的子目录中的新重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆