New rewrite rules in subdirectory with htaccess

2019-08-24 14:29发布

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.

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.

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

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]

1条回答
Fickle 薄情
2楼-- · 2019-08-24 14:56

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

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]
查看更多
登录 后发表回答