Apache Redirect in htaccess to subdirectory

≯℡__Kan透↙ 提交于 2019-12-02 07:52:09

问题


I've built an in house app that needs to start in public folder, directory structure is as follows

myapp/conf
myapp/tmp
myapp/doc
myapp/public
myapp/public/css
myapp/public/js
myapp/public/index.php

If a user installs it in webroot subdirectory, instead of making myapp/public the webroot, then the URLs would be

somehost/myapp/public/index.php

How do I redirect with /myapp/.htaccess, so that it forwards everything to /public/index.php BUT also changes the URL to include /public.

I have figured out how to redirect to public, but since i'm doing URL Rewrites in /public/, i need $_SERVER['REQUEST_URL'] to have /public/ in the path.

I'd like to prevent access to everything else (/conf and /doc etc..)

any luck?

What i tried:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) index.php/$1 [R=301,L]
RewriteRule ^$ public [L]
</IfModule>

But this seems to put public before myapp and the url looks like this: localhost/public/myapp/, instead I need localhost/myapp/public/

In simpler terms, I want all URLS to /whatever/* to have public appended to it /whatever/public/*.

The solution needs to be independent of the domain name and 'myapp' name. They could name the root folder ('myapp') whatever but it will always have a sub dir called 'public'.


回答1:


Try

RewriteEngine On
RewriteBase /myapp/

RewriteCond %{REQUEST_URI} !/public/
RewriteRule (.*) public/index.php/$1 [R=301,L]
RewriteRule ^$ public [L]

The main thing is you need to change the base to your app name. Then the rules can remain the same. The condition is needed to prevent redirect looping. But you probably won't need it if you have an .htaccess file handling requests inside /public/.



来源:https://stackoverflow.com/questions/11893738/apache-redirect-in-htaccess-to-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!