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

问题描述

我有以下结构

-www
  \-subfolder

www 中,我有我的主站点的 index.php.

In www I have my main site's index.php.

在子文件夹中,我有一种管理 UI,在那里我想有另一个 index.php 用于管理 UI.

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 并且基本上我的管理 UI 页面不显示.

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]

你能帮我吗?我在其他答案中尝试了几个选项,但由于我不是一个高级的网络开发人员,所以我无法工作.

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 服务器,它为我提供了 Apache 服务器.在 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.php127.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/

我有站点本地化,所以 URL 被构造为 /$lang/$page 例如<a href="/<?php echo $lang; ?>/home ">首页

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/

All my main site's pages lie in www/html/$lang/

$_SERVER['REQUEST_URI']) 给出页面 HOME 的 /en/home.

$_SERVER['REQUEST_URI']) gives /en/home for page HOME.

127.0.0.1/en/home 工作

但是,我创建了一个管理 UI,它位于 www/admin 文件夹中 - www 中的下一级.

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

在那里我没有任何本地化.我只有 EN 作为语言.所以在 admin 文件夹中 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' ), '/' );

但是,这里的导航如下

However, here navigation is as follows <a href=/admin/home"> HOME </a>

在此基础上,我可以在 admin 文件夹中的 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/admin/html

这根本行不通.每当我在管理 UI 中按下主页链接时,我都会被重定向到我的主站点(不存在的页面).如果我在 .htaccess 中添加 RewriteRule ^subfolder/- [L],我会收到 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!

推荐答案

我由此假设您的 www 目录中只有一个 .htaccess 文件.

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

想想规则是什么

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

在来自 www 的 Perdir 上下文中解释时:采用 someDir/somePage 形式的任何 URI 并将其替换为 index.php?lang=someDir&page=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 处理将优先使用请求路径上最低的 .htaccess 文件设置 RewriteEngine On.因此,如果您在子文件夹"子文件夹中添加特定于管理员的 .htaccess 文件,这将抢占 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.

当真正的问题是如果我的网站由共享服务托管,我如何调试我的 .htaccess 规则?"时,Veni 和其他发帖人会进入问答环节.我添加共享服务资格的原因是,如果您对 LAMP 配置具有 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 访问权限,并且托管服务提供商出于性能原因禁用了此类日志记录,因此他们有一个二进制反馈——规则工作或他们不工作.我使用共享服务,我的方法(在此处描述)是设置一个 VM将此配置镜像为测试和集成环境 - 在此我有这样的 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.

具体点:

  • 如果您的结构是 /(lang|admin)/page,那么为什么要制定此规则,因为它会导致 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)

    我是 Stackoverflow 的新手,但不会整理这种 s**t.我的博客上有几篇关于此的详细文章.

    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 在您的 windows\system32\drivers\etc\hosts 中放置 127.0.0.1 的别名
    • 您可以在 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(并停止重写循环)-/和/admin/的默认值- 行政/*- */*- *(和语言默认值)

    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 子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-28 08:29