英文:
How to setup a htaccess to a subfolder
问题
I have this .htaccess right now:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
It works if I set up my script in the root folder. However, if I create a new folder called 'api' and move all my script there, it doesn't work as expected, and I see a blank page.
I modified the .htaccess like this:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/index.php [QSA]
英文:
I have this .htaccess right now:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
It works if i setup my script on root folder, but if i create a new folder called 'api'
and move all my script to there, does not work as expected and i see a blank page.
I modified the .htacess like this:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/index.php [QSA]
答案1
得分: 2
很不幸,从配置中并不完全清楚,但有两种不同的路径机制:
RewriteBase /api/
这是“相对于”请求(或浏览器)的。即 https://example.com/api/
然而...
RewriteRule ^(.*)$ /api/index.php [QSA]
这是相对于服务器的。因此,在服务器内部,你需要在名为 'api' 的根目录中确实有这个文件。
简而言之,我认为你需要的是:
RewriteRule ^(.*)$ api/index.php [QSA]
(注意缺少的起始斜杠 - 这意味着相对于文档根目录而不是绝对服务器路径)
另外,使用 RewriteBase /api/
,实际上意味着你不关心其他(未定义的)根路径(例如 /api2/xx
不会匹配)。因此,解决你最初的问题可能有更简单的方法:只需将你最初的 .htaccess
文件移到 api 目录中。
英文:
Unfortunately, it is not entirely clear from the config, but there are two different path mechanisms:
RewriteBase /api/
This is "relative" to the request (or browser). i.e. https://example.com/api/
However...
RewriteRule ^(.*)$ /api/index.php [QSA]
That is 'relative' to the server. So inside the server, you would need to have literally that file in a root directory called 'api'.
In short, I believe that what you need is:
RewriteRule ^(.*)$ api/index.php [QSA]
(note the missing starting slash - which means path relative to the document root and not absolute server path)
On a side-note, with RewriteBase /api/
, it effectively means you do not care about other (undefined) root paths (e.g. /api2/xx
won't be matched). Therefore there may be an even easier way to your original problem: just move your original .htaccess
file into the api directory.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论