的.htaccess的子文件夹 [英] .htaccess for subfolder

查看:242
本文介绍了的.htaccess的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的结构

-www
  \-subfolder

WWW 我有我的主网站的的index.php

在子文件夹中我有一种管理界面,并在那里我想有另一个的index.php 的管理界面。

In subfolder I have a sort of admin UI and in there I'd like to have another index.php for the admin UI.

目前从内部 /subfolder/index.php 重定向到 WWW / index.php文件,基本上我的要求我的管理界面的网页不会显示出来。

Currently my requests from within /subfolder/index.php get redirected to www/index.php and basically the pages of my admin UI don't show up.

这是我的的.htaccess 文件:

RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

您能帮我吗?我试过几种选择在其他的答案,但因为我没有那么先进的web开发者,我无法得到任何工作。

Can you help me? I've tried several options in other answers but as I'm no so advanced a web developer, I couldn't get any to work.

@TerryE,很抱歉,如果我来过粗。

@TerryE, Sorry if I have come off as crude.

我使用的是本地设置进行测试。 我已经安装了Vertrigo服务器,这给了我阿帕奇server.Running在Windows7操作系统。该服务器安装在Program Files文件\的VertrigoServ \ Apache的文件夹。

I am using a local setup for testing. I have installed Vertrigo server, which gives me Apache server.Running on Windows7 OS. The server is installed in Program files\VertrigoServ\Apache folder.

我的公用文件夹为www。在那里我有我的主要网站定义。 。该网站是通过 127.0.0.1/index.php 127.0.0.1 /

My public folder is www. In there I have my main site definition. . The site is accessed locally via 127.0.0.1/index.php or 127.0.0.1/

我的网站定位,以便网址为 / $ LANG / $页如建< A HREF =/<?PHP的echo $ LANG;?> /家> HOME< / A>

I have site localization so URLs are constructed as /$lang/$page e.g. <a href=" / <?php echo $lang; ?> / home "> HOME </a>

的index.php 主网站的我有以下几点:

In index.php of main site I have the following:

$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );

$lang = trim( ( isset( $_GET[ 'lang' ] ) ? $_GET[ 'lang' ] : 'en' ), '/' );
$langs = array( 'en', 'fr', 'ru' ); 

和在这个数据我可以打开网页是这样的:

And upon this data I get to open the pages this way:

include 'html/'. $lang . '/' . $page . '.php';

我所有的主要网站的网页存在于 www / html等/ $ LANG /

$ _ SERVER ['REQUEST_URI']) / EN /家庭的主页。

127.0.0.1/en/home

不过,我已经创建了一个管理界面而在于文件夹WWW /管理员 - 的下一级的WWW。

However I have created an admin UI which lies in folder www/admin - one level below in www.

,并在那里我没有任何本地化。我只是有EN的语言。 因此,在管理文件夹我又有了index.php文件顶部

And in there I don't have any localization. I just have EN as language. So at the top of the index.php in admin folder I have again

$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );

不过,在这里航行如下:&LT; A HREF = /管理/家&GT; HOME&LT; / A&GT;

和在此我可以构建在管理文件夹中的index.php页面如下:

and upon this I get to construct the pages in the index.php in admin folder as follows:

include 'html/ . $page . '.php';

页面在于 WWW /管理/ HTML

这并不在所有的工作。每当我美元的管理界面p $ PSS主页链接,我重定向到我的主网站(不存在的页面)。如果我添加重写规则^子文件夹/ - [L] 在.htaccess,我得到 HTTP 404 NOT FOUND错误

This does not work at all. Whenever I press home link in admin UI, I get redirected to my main site (non-existing page). If I add RewriteRule ^subfolder/ - [L] in .htaccess, I get HTTP 404 NOT Found error.

127.0.0.1/admin/home 不起作用。无论是否有任何其他的导航从内部管理。感谢您的意愿和耐心帮我!

127.0.0.1/admin/home DOES NOT WORK. Neither does any other navigation from within admin. Thank you for your willingness and patience to help me!

推荐答案

我想从这个,你只能有一个单一的的.htaccess 文件在您的 WWW 目录。

I assume from this that you only have a single .htaccess file in your www directory.

想想规则

RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

确实从一个Perdir方面PTED跨时$ P $ WWW :采取何种形式的任何URI someDir / SomePage的和更换的index.php LANG = someDir和放大器;页面= SomePage的,因此这将截获并重写任何 /subfolder/index.php

does when interpreted in a Perdir context from www: take any URI of the form someDir/somePage and replace it by index.php?lang=someDir&page=somePage, so this will intercept and rewrite any /subfolder/index.php.

这是没有很好的记载,但Perdir处理将preferentially用最低的的.htaccess 文件设置 RewriteEngine叙述在的请求路径。所以,如果你在子文件夹子文件夹添加一个管理员专用的的.htaccess 的文件,这将preempt在 WWW 之一,解决这个问题。

This isn't well documented but Perdir processing will preferentially use the lowest .htaccess file setting RewriteEngine On on the request path. So if you add an admin-specific .htaccess file in the "subfolder" subfolder, this will preempt the www one and circumvent this problem.

VENI和其他海报得到一个Q&安培;?我怎么调试我的.htaccess规则,如果我在我的网站是由一个共享服务托管A时,真正的问题是一个我添加共享服务资格的原因是,如果你有root访问你的灯的配置,那么你可以打开重写日志和日志文件在4-6调试级别会给你足够的取证工作了是怎么回事

Veni and other posters get in a Q&A when the real issue is one of "how do I debug my .htaccess rules if I my website is hosted by a shared service?" The reason that I add the shared service qualification is that if you have root access to your LAMP config then you can turn on Rewrite logging and the logfile at a debug level of 4-6 will give you enough forensics to work out what is going on.

不过,绝大多数爱好/小型服务用户购买他们的服务在共享的基础上,并在这里,他们没有root访问权限和托管服务提供商禁止伐木等性能方面的原因,使他们有一个二进制反馈 - 规则工作或他们不这样做。我使用一个共享的服务和我的方法(描述这里)是建立一个虚拟机这反映这种配置的测试及集成环境 - 在此我有这样的root访问权限。然而,这可能是过于复杂,对于大多数用户。这的确是一个更广泛的话题,值得自己的Q /讨论。

However, the large majority of hobby / small service users buy their services on a shared basis and here they don't have root access and the hosting provider disables such logging for performance reasons so they have a binary feedback -- the rules work or they don't. I use a shared service and my approach (described here) is to set up a VM which mirrors this configuration for as a test and integration environment -- and in this I have such root access. However, this is probably far too complicated for most users. This is really a wider topic that merits its own Q / discussion.

在具体点:

  • 如果你的结构是 /(郎|管理员)。/页,那你为什么有这样的规则,因为它可能会导致严重的破坏perdir重试

  • If your structure is /(lang|admin)/page, then why do you have this rule because it can cause havoc on perdir retries.

RewriteRule ^$ index.php [QSA,L]

  • 更​​好的类似下面的强制重定向到默认的语言(假设郎名单EN和IT在这个例子:

  • Better something like the following to force a redirect to a default language (assume the lang list is EN and IT in this example:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond $1 !^/(en|it|admin)/
    RewriteRule ^(.*) http://yourdomain/en/$1 [R=301,L]
    

  • 您知道重定向重新启动/循环问题? (搜索REDIRECT_STATUS)

  • Are you aware of the redirect restart /looping issues? (search for REDIRECT_STATUS)

    我是新来的#1,而不是梳理这种第**吨。我有一对夫妇的详细的文章在我的博客这一点。

    I'm new to Stackoverflow, but not to sorting out this sort of s**t. I've got a couple of detailed articles on my blog on this.

    确定一些一般性的帮助。

    OK some general help.

    • 请不要我们的固定IP地址把一个别名127.0.0.1在您的WINDOWS \ SYSTEM32 \ DRIVERS \等\主机
    • 您可以改写记录在你的VertrigoServ Apache的配置转向,这给你的,你有问题的详细审核
    • 这也帮助,如果你有一点点的诊断存根,以帮助您了解正在发生的事情,这就是我在测试目录想出了的index.php:

    • Don't us fixed IPs put an alias for 127.0.0.1 in your windows\system32\drivers\etc\hosts
    • You can turn on rewrite logging in your VertrigoServ Apache config and this gives you a detailed audit of where you have problems
    • It also helps if you have a little diagnostic stub to help you understand what is going on and this is what I came up with for index.php in the test directories:

    <?php header( "Content-type: text.plain" ); echo "main: "; var_export($_GET);
    

  • 但你真的需要为每一个有下列情形:   - 处理URI存在(并停止改写循环)   - 默认为/和/管理/   - 管理/ *   - * / *   - *(和语言默认)

    But you really need for each of the following cases: - to handle the URI exists (and stop rewrite loops) - the defaults for / and /admin/ - admin/* - */* - * (and the language defaults)

    这就是我来到了。我已经把它简单。我们可以使用复杂的正则表达式折叠其中的一些,但何必呢。如果需要,您可能需要增加更多的QSA。

    and this is what I came up with. I've kept it simple. We could use complex regexps to fold some of these but why bother. You may need to add more QSAs if needed.

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -f                [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .  -                                  [L]
    
    RewriteRule ^$ index.php                          [QSA,L]
    RewriteRule ^admin/?$ admin/index.php             [QSA,L]
    
    RewriteRule ^admin/(.*)$ admin/index.php?page=$1  [L]
    
    RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
    
    RewriteRule ^(.*)$ index.php?lang=en&page=$1      [L]
    

    这篇关于的.htaccess的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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