简体   繁体   中英

Apache Rewrite htaccess in subdir

let me get to the point.

I have a php project that has this structure

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

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? putting the .htaccess in the wrong directory? I have to use multiple .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

Here are the .htaccess
domain.com/api/ .htaccess file

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

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:

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

So this should redirect internally to the subfolder public

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

Update1

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

I did some test on XAMPP an got to following result

Put your .htaccess in the ROOT folder , delete all other .htaccess files in subfolders . Rename api to _api or something, as you like

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

add this .htaccess to 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

This scenario worked on my test system as expected. Give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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