【问题标题】:.htaccess Multiple subdirectory rewriting to www-root.htaccess 多个子目录重写为 www-root
【发布时间】:2013-05-11 07:35:00
【问题描述】:

我有以下文件夹结构:

  • www-root
    • 后端
      • 配置
    • 前端
      • 管理
      • 店面

我希望能够从主 url 访问目录并隐藏其间的子目录。

所以我应该可以像这样访问管理部分:

 http://localhost/Administration/

主页面存储在子目录“StoreFront”中,我希望能够从根目录访问:

 http://localhost

到目前为止,这是我的 .htaccess 文件中的代码:

# Store Redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/StoreFront/$1
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^(/)?$ /Frontend/StoreFront/index.php [L]
RewriteCond %{REQUEST_URI} !^/Administration
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/Administration/$2

但此代码无法正常工作。它将除 index.php 文件之外的所有文件重写到 Administration 子目录。附注:后端目录中的 php 文件应从前端保持“可包含”。

【问题讨论】:

    标签: .htaccess web directory rewrite subdirectory


    【解决方案1】:

    让我先告诉你,你想要实现的是不可能完成的任务,现在让我告诉你原因。你有这个规则:

    RewriteRule ^(.*)$ /Frontend/StoreFront/$1
    

    你有:

    RewriteRule ^(.*)$ /Frontend/Administration/$2
    

    你不能让.* 去这两个地方。您需要以某种方式区分这两条路径。

    除此之外,您还有其他问题,例如:

    1. 不在需要的地方使用L(LAST)标志
    2. 在第二条规则中使用 $2 而不是 $1

    编辑:根据您的 cmets:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(Administration(?:/.*|))$ /Frontend/$1 [L,NC]
    
    RewriteCond %{HTTP_HOST} ^localhost [NC]
    RewriteRule ^$ /Frontend/StoreFront/index.php [L]
    
    RewriteCond %{HTTP_HOST} ^localhost [NC]
    RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /Frontend/StoreFront/$1 [L]
    

    【讨论】:

    • 第二个我只想将管理目录重写为/Frontend/Administration/。我不想将管理目录中的每个文件都重写到根目录。只是目录本身。所以http://localhost/Administration 实际上是:http://localhost/Frontend/Administration/,因此在浏览器中显示为http://localhost/Administration
    • 好的,我将根据此提供我的答案。
    • 谢谢!我非常感激。不过,我还有一个小问题。如果我在管理后不加斜线,我会得到 404。所以http://localhost/Administration 会产生 404,但http://localhost/Administration/ 不会。
    • 几乎可以工作,只是如果现在我不在它后面加上斜线,它会在 url 字段中显示http://localhost/Frontend/Administration/。该页面显示正常,只是浏览器字段中的 url 错误。只有当它后面没有斜线时才会发生……如果后面有斜线,它只会显示http://localhost/Administration/
    • 这很奇怪,因为我没有在任何地方使用R 标志。你可以试试这个规则吗:RewriteRule ^(Administration.*)$ /Frontend/$1 [L,NC]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2013-07-12
    • 2018-01-31
    • 2023-03-19
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多