【问题标题】:.htaccess, hide sub directories.htaccess,隐藏子目录
【发布时间】:2017-12-07 22:10:27
【问题描述】:

我刚开始学习 .htaccess 的工作原理,我正在努力让它按我想要的方式工作。

这是我当前的 .htaccess 文件:

RewriteOptions inherit

#ErrorDocument 404 https://tsrvtc.com/_pages/404page.html
#ErrorDocument 500 https://tsrvtc.com/_pages/500page.html
#ErrorDocument 403 https://tsrvtc.com/_pages/403page.html

<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on

RedirectMatch 301 ^/?$ https://www.tsrvtc.com/index

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+_pages/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^_pages/)^(.*)$ /_pages/$1 [L,NC]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+vtc_manager/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^vtc_manager/)^(.*)$ /vtc_manager/$1 [L,NC]

RewriteCond %{HTTP_HOST} ^tsrvtc\.com$ [NC]
RewriteRule ^(.*)$ http://www.tsrvtc.com/$1 [R=301,L]

</IfModule>
AuthName "public"
AuthUserFile "/home/tsrvtcco/.htpasswds/public_html/passwd"

它目前可以在 url 中隐藏 _pages 目录,但是当我尝试隐藏 vtc_managerm 目录然后转到该目录中的页面时,它会重定向到我的域名并将 index.php 放在最后。我收到此错误:https://gyazo.com/0cc504188657beafd2042d6332e4bd9b

这是目录的布局:

public_html 
  _pages
  vtc_manager

任何帮助将不胜感激。

【问题讨论】:

  • 你可以举一个访问vtc_manager时请求url的例子吗?
  • 嗨 Ben,我使用的 URL 是 www.tsrvtc.com/vtc_manager/index.php。 index.php 文件位于 vtc_manager 目录中,在 public_html 目录中,主页是 index.html。我什至尝试将 index.html 重命名为 home.html,但它仍然给我他 gyazo 链接中的错误。
  • 您能解释一下您的期望吗?您是否要同时将/ 设为/_pages/vtc_manager 的别名?
  • 我只是想从 URL 中隐藏 '/_pages' 和 '/vtc_manager' 目录。
  • 您将/ 设为/_pages 的别名,并且无法为另一个页面创建别名,在这种情况下为/vtc_manager。由于重定向配置错误,您正在使 .htaccess 无限重定向,因此它会导致内部服务器错误。

标签: .htaccess mod-rewrite


【解决方案1】:

由于 .htaccess 配置错误导致的错误。当您尝试深入研究时,apache 日志显示此错误:

请求超出了 10 次内部重定向的限制,因为可能 配置错误。使用“LimitInternalRecursion”来增加 必要时限制。使用“LogLevel debug”获取回溯。

根据这些条件和规则,您进行了无限重定向:

1. RewriteCond %{REQUEST_FILENAME} !-f
2. RewriteRule (?!^_pages/)^(.*)$ /_pages/$1 [L,NC]

3. RewriteCond %{REQUEST_FILENAME} !-f
4. RewriteRule (?!^vtc_manager/)^(.*)$ /vtc_manager/$1 [L,NC]

当您发出请求/ 并且在根目录中找不到文件时,则第一个条件(1.)满足并且规则满足。请求 uri 被重写为/_pages/。重写的请求被处理,然后再次交回 URL 解析引擎。第二次尝试,第一个条件(1.)满足,但规则模式(2.)不满足。但是,第二个条件 (3.) 满足并且规则 (4.) 满足。然后请求 uri 被重写为/vtc_manager/_pages/。然后处理重写的请求等等。

您应该完整阅读mod_rewrite moduleRewriteRule Flags 的文档以了解您要做什么。

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2017-08-03
    • 2015-07-10
    • 2023-03-11
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多