Apache 在 htaccess 中重定向到子目录 [英] Apache Redirect in htaccess to subdirectory

查看:26
本文介绍了Apache 在 htaccess 中重定向到子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个需要在公共文件夹中启动的内部应用程序,目录结构如下

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

如果用户将其安装在 webroot 子目录中,而不是将 myapp/public 设为 webroot,那么 URL 将是

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

somehost/myapp/public/index.php

我如何使用/myapp/.htaccess 重定向,以便将所有内容转发到/public/index.php 但同时更改 URL 以包含/public.

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

我已经知道如何重定向到 public,但是由于我在/public/中进行 URL 重写,所以我需要 $_SERVER['REQUEST_URL'] 在路径中包含/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.

我想阻止访问其他所有内容(/conf 和/doc 等)

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

运气好吗?

我尝试了什么:

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

但这似乎把 public 放在 myapp 之前,url 看起来像这样:localhost/public/myapp/,而不是我需要 localhost/myapp/public/

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

简单来说,我希望/whatever/* 的所有 URL 都附加到/whatever/public/*.

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

解决方案需要独立于域名和'myapp'名称.他们可以随意命名根文件夹(myapp"),但它总是有一个名为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'.

推荐答案

尝试

RewriteEngine On
RewriteBase /myapp/

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

最重要的是您需要将基础更改为您的应用程序名称.那么规则可以保持不变.需要该条件来防止重定向循环.但是如果您在/public/中有一个 .htaccess 文件处理请求,您可能不需要它.

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/.

这篇关于Apache 在 htaccess 中重定向到子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆