简体   繁体   中英

.htaccess reroute to sub-directory

I have site I am running on localhost http://example and I have directory in the root that contains my api so it's http://example/api

In the api folder I have my root directory for the api which is public directory. so /api/public/... .

I want to have a .htaccess that redirects the user to the public directory when they hit http://example/api/{api parameters} . So I can have a clean url without having to write this http://example/api/public/{api parameters}

I tried this but to no avail

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^api/(.*) /api/public/$1 [L]

I got the above from a SO post but still could not get it to work.

Try using:

RewriteEngine On
RewriteBase /api/public/

Quoting a stack overflow answer , "By having the RewriteBase there, you make the relative path come off the RewriteBase parameter."

With this method I believe you will have to rewrite all paths that don't fall under the base directory. If this is a problem, you could create a rule for each page.

Update: You might be able to write a rule that checked for files at that path before/after it checks for the relative path.

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