【问题标题】:how to redirect to sub directory in htaccess without direct access to it如何在不直接访问它的情况下重定向到 htaccess 中的子目录
【发布时间】:2026-02-14 18:05:02
【问题描述】:

我已经问过这个问题但没有答案。

我开发了新论坛,所以我将一些文件,如索引、登录、注册等...放在名为 webroot 的子目录中,我想将所有请求重定向到 webroot 而不直接访问它,例如:

localhost/myforum [根项目文件] 应该可以正常访问,但是

localhost/myforum/webroot 重定向到 404

我的论坛/.htaccess

DirectoryIndex index.php

# No directory listings
IndexIgnore *

<IfModule mod_rewrite.c>
    RewriteEngine on

   # Uncomment the following line if your webserver's URL is not directly related to physical file paths.
   # Also uncomment it if you are on 1&1 hosting
   #RewriteBase /

    # Comment the following line if you have not a .well-known directory in the root folder.
    RewriteRule ^(\.well-known/.*)$ $1 [L]

    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

我的论坛/webroot/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    myforum/webroot/.htaccess作为这个代码:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # return 404 if accessed directly
        RewriteCond %{THE_REQUEST} /webroot/ [NC]
        RewriteRule ^ - [L,R=404]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

    【讨论】:

    • 非常感谢,但它帮助了我
    最近更新 更多