如何使用 .htaccess 将所有请求映射到子目录

How to map all requests to a subdirectory using .htaccess

我有这样的结构:

root
-web
-- .htaccess
-- index.html
-- robots.txt
-.htaccess

我的文档根目录是 root 目录。 /root/.htaccess 的内容是:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine on

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

现在,/robots.txt 和 /web/robots.txt 都指向一个文件,即 /web/ 下有整个网站的副本。

如何在 .htaccess 文件中制定这样的规则,使 /web/robots.txt 指向 /web/web/robots.txt(并显示 404)?我只需要使 /robots.txt 工作。

解决方案必须仅使用 .htaccess。

(问题不在于 robots.txt 文件,这只是一个例子)

谢谢!

像这样更改您的根 .htaccess:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine on

    # otherwise route /foobar to /web/foobar
    RewriteRule ^((?!web/).*)$ web/ [L,NC]
</IfModule>

这将避免添加 web/ 如果请求已经有了它。

/web/.htaccess 内将此规则作为 第一条规则:

RewriteEngine On

# show 404 for direct requests to /web    
RewriteCond %{THE_REQUEST} /web [NC]
RewriteRule ^ - [L,R=404]