简体   繁体   English

Apache在subdir中重写htaccess

[英]Apache Rewrite htaccess in subdir

let me get to the point. 让我说清楚。

I have a php project that has this structure 我有一个具有这种结构的php项目

ROOT
.htaccess
--api/
----public/
------index.php
------test.html
--backoffice/

And what I want is to use the index.php of api/public to get it working like 我想要的是使用api / public的index.php使它像

domain.com/api/index.php 

instead of 代替

domain.com/api/public/index.php 

I've been trying with no success the rewritebase or rewriterules of .htaccess, I'm missing something? 我一直在尝试.htaccess的rewritebase或rewriterules没有成功,我缺少什么吗? putting the .htaccess in the wrong directory? 将.htaccess放在错误的目录中? I have to use multiple .htaccess? 我必须使用多个.htaccess吗?

UPDATED : So I got my two .htaccess files, by now I'm able to access with the url domain.com/api/index.php but it throws 401 , it's something... on the other hand I can access to the file test.html located under /api/public with the current url domain.com/api/test.html 更新 :所以我得到了两个.htaccess文件,现在我可以使用url domain.com/api/index.php进行访问,但它抛出401,这是某件事...另一方面,我可以访问文件/test.html,位于/ api / public下,当前网址为domain.com/api/test.html

Here are the .htaccess 这是.htaccess文件
domain.com/api/ .htaccess file domain.com/api/ .htaccess文件

RewriteEngine On
RewriteBase /api/public/
RewriteCond %{REQUEST_URI} !^/api/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/public/$1

domain.com/api/public/ .htaccess file domain.com/api/public/ .htaccess文件

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

I think you can use a seperate .htaccess file in your API folder with a rule like this: 我认为您可以使用如下规则in your API folder使用单独的.htaccess文件:

RewriteRule ^$ /public [R=301,L]

So this should redirect internally to the subfolder public 因此,这应该在内部重定向到子文件夹public

ROOT
  | --api/
  |     |-- .htaccess 
  |     |-- public/
  |             |-- index.php
  |-- backoffice/

Update1 更新1

I think in your case this should work 我认为您应该这样做

RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ /api/public/$1 [L]

It depends also on the System which you are using and the preset. 它还取决于您使用的系统和预设。

Update2 更新2

I did some test on XAMPP an got to following result 我在XAMPP上做了一些测试,得出以下结果

Put your .htaccess in the ROOT folder , delete all other .htaccess files in subfolders . 将您的.htaccess放在ROOT folderdelete .htaccess files in subfolders所有其他.htaccess files in subfolders Rename api to _api or something, as you like 根据需要将api重命名为_api或其他名称

ROOT
  | --.htaccess
  | --/_api/  <<<------- rename
  |     |-- /public/
  |             |-- index.php
  |-- /backoffice/

add this .htaccess to root 将此.htaccess添加到root

RewriteEngine On
Options FollowSymLinks

RewriteBase /
DirectoryIndex index.php index.html

RewriteRule ^api2/(.*)$ /api/public/$1 [B,L,QSA]

Call as before yoursite.com/api yoursite.com/api之前一样致电

This scenario worked on my test system as expected. 这种情况在我的测试系统上按预期工作。 Give it a try. 试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM