使用 htaccess 阻止从子域访问除单个 URI 之外的所有 URI
Block access to all but a single URI with htaccess from a subdomain
我正在尝试使用 htaccess
来阻止访问我的站点,除非请求转到特定的 URI,但仅当从某个子域访问时才如此。
基本上我有许多子域,我用它们来绕过浏览器请求限制,以加快包含许多图像的页面的加载速度。我已经测试过,这运行良好,防止浏览器阻塞请求,直到其他人完成。
我想要实现的是阻止从这些子域访问站点的其余部分,因此它们只能访问以 /images
开头的 URI。这个想法是为了避免我的页面或子域的重复 URL 被意外索引到 Google.
尽我所能,我做不对,这是我目前的情况:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Prevent access to anything but the /images URI from subdomains
RewriteCond %{HTTP_HOST} ^cdn[1-9]\.domain.localhost$ [NC]
RewriteRule !^/images.* - [R=404]
</IfModule>
我希望子域访问的 URI 是:
domain.localhost/images/(products|retailers)/400/400/filename.jpg
您可能已经猜到这不是物理文件夹结构,它是我的 Laravel PHP 应用程序中的一条路径。
根据我的理解,底部规则应该是“如果请求来自匹配的域,并且请求 URI 不以 /images
开头,则重定向到 404 错误,但因为它不起作用我一定离题很远。我认为问题可能是由于规则的顺序引起的,但我不确定。
有什么建议吗?
RewriteRule
指令接收每个目录的 URI。因此,它接收到的路径将不包含前导斜杠 (/
)。这仅适用于在服务器或虚拟主机上下文中完成的重写。
我建议您使用 %{REQUEST_URI}
变量代替您的规则:
RewriteCond %{HTTP_HOST} ^cdn[1-9]\.domain\.localhost$ [NC]
RewriteCond %{REQUEST_URI} !^/images\b [NC]
RewriteRule ^ - [R=404]
我正在尝试使用 htaccess
来阻止访问我的站点,除非请求转到特定的 URI,但仅当从某个子域访问时才如此。
基本上我有许多子域,我用它们来绕过浏览器请求限制,以加快包含许多图像的页面的加载速度。我已经测试过,这运行良好,防止浏览器阻塞请求,直到其他人完成。
我想要实现的是阻止从这些子域访问站点的其余部分,因此它们只能访问以 /images
开头的 URI。这个想法是为了避免我的页面或子域的重复 URL 被意外索引到 Google.
尽我所能,我做不对,这是我目前的情况:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Prevent access to anything but the /images URI from subdomains
RewriteCond %{HTTP_HOST} ^cdn[1-9]\.domain.localhost$ [NC]
RewriteRule !^/images.* - [R=404]
</IfModule>
我希望子域访问的 URI 是:
domain.localhost/images/(products|retailers)/400/400/filename.jpg
您可能已经猜到这不是物理文件夹结构,它是我的 Laravel PHP 应用程序中的一条路径。
根据我的理解,底部规则应该是“如果请求来自匹配的域,并且请求 URI 不以 /images
开头,则重定向到 404 错误,但因为它不起作用我一定离题很远。我认为问题可能是由于规则的顺序引起的,但我不确定。
有什么建议吗?
RewriteRule
指令接收每个目录的 URI。因此,它接收到的路径将不包含前导斜杠 (/
)。这仅适用于在服务器或虚拟主机上下文中完成的重写。
我建议您使用 %{REQUEST_URI}
变量代替您的规则:
RewriteCond %{HTTP_HOST} ^cdn[1-9]\.domain\.localhost$ [NC]
RewriteCond %{REQUEST_URI} !^/images\b [NC]
RewriteRule ^ - [R=404]