.htaccess:阻止物理目录 url,但允许该 url 间接通过子域

发布于 2024-09-10 20:46:08 字数 478 浏览 1 评论 0原文

因此,出于安全原因,我想禁止 http://www.domain.com/directory/ 但仅允许通过 http://subdomain.domain.com/directory/ 访问该物理目录

简单的方法是只移动目录,但我不能这样做,因为它会破坏应用程序,那么我该如何使用 .htaccess 来做到这一点?

我已经使用以下方法暂时阻止了它:

RedirectMatch 301 ^/directory/$ http://www.domain.com/

但是当然,在任何一种情况下都会重定向子目录,所以我想我可以只输入:

RedirectMatch 301 ^http://www.domain.com/directory/$ http://www.domain.com/

或类似的东西,但它不起作用...建议?

So for security reasons, I want to disallow http://www.domain.com/directory/ but allow that physical directory only through http://subdomain.domain.com/directory/

The simple way would be to just move the directory, but I can't do it because it breaks the application, so how would I do this with .htaccess?

I've temporarily blocked it using:

RedirectMatch 301 ^/directory/$ http://www.domain.com/

But of course that redirects the subdirectory in either case, so what I'd think is I could just put:

RedirectMatch 301 ^http://www.domain.com/directory/$ http://www.domain.com/

or something similar but it doesn't work... suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-09-17 20:46:08

您可以使用 RewriteCond 来完成此操作:

RewriteEngine on
RewriteCond %{HTTP_HOST} !subdomain.domain.com
RewriteRule ^directory http://www.domain.com [R=301]

虽然更好,但我建议使用相同的文档根创建另一个虚拟主机。因此,在您的主服务器配置中,

<VirtualHost *:80>
    # this is the existing virtual host for the www subdomain
    ServerName www.domain.com
    ...
    DocumentRoot /path/to/docroot
    # Add this next section
    <Directory /path/to/docroot/directory>
        Options allow,deny
        Deny from all
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    # this is the existing vhost for the other subdomain
    ServerName subdomain.domain.com
    ...
    DocumentRoot /path/to/docroot
</VirtualHost>

当 Apache 不必解析 .htaccess 文件时,您的效率会更高。

You can do it with a RewriteCond:

RewriteEngine on
RewriteCond %{HTTP_HOST} !subdomain.domain.com
RewriteRule ^directory http://www.domain.com [R=301]

Although better yet, I would recommend creating another virtual host with the same document root. So in your main server configuration, you'd have

<VirtualHost *:80>
    # this is the existing virtual host for the www subdomain
    ServerName www.domain.com
    ...
    DocumentRoot /path/to/docroot
    # Add this next section
    <Directory /path/to/docroot/directory>
        Options allow,deny
        Deny from all
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    # this is the existing vhost for the other subdomain
    ServerName subdomain.domain.com
    ...
    DocumentRoot /path/to/docroot
</VirtualHost>

It's more efficient when Apache doesn't have to parse the .htaccess files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文