.htaccess return 404 当不是子域上的图像文件时
.htaccess return 404 when not image file on subdomain
我有一个 .htaccess 文件,如果子域是 img0.example.info
、img1.example.info
、img2.example.info
等,它应该强制只提供图像文件。我想在 404.html 中提供带有 404 响应代码的描述性标记,但它只显示 500 Internal Server Error 错误。
这是我的 .htaccess 文件的内容:
<IfModule mod_rewrite.c>
RewriteEngine on
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ - [R=404,L]
ErrorDocument 404 /404.html
</IfModule>
它在 .jpg、.jpeg、.gif 和 .png 文件都可以正常提供但我无法收到整洁的 404 消息时部分起作用。请帮忙。
您的 /404.html
也受此规则约束。
试试这个代码:
ErrorDocument 404 /404.html
<IfModule mod_rewrite.c>
RewriteEngine on
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
RewriteRule !^404\.html$ - [R=404,L,NC]
</IfModule>
另一个选项,也就是重定向到 404 文件。
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !(.+)\.(jpg|jpeg|gif|png)$ [NC]
RewriteCond %{REQUEST_URI} !^/404.html$ [NC]
RewriteRule ^(.*)$ /404.html [R=404,L]
我有一个 .htaccess 文件,如果子域是 img0.example.info
、img1.example.info
、img2.example.info
等,它应该强制只提供图像文件。我想在 404.html 中提供带有 404 响应代码的描述性标记,但它只显示 500 Internal Server Error 错误。
这是我的 .htaccess 文件的内容:
<IfModule mod_rewrite.c>
RewriteEngine on
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ - [R=404,L]
ErrorDocument 404 /404.html
</IfModule>
它在 .jpg、.jpeg、.gif 和 .png 文件都可以正常提供但我无法收到整洁的 404 消息时部分起作用。请帮忙。
您的 /404.html
也受此规则约束。
试试这个代码:
ErrorDocument 404 /404.html
<IfModule mod_rewrite.c>
RewriteEngine on
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
RewriteRule !^404\.html$ - [R=404,L,NC]
</IfModule>
另一个选项,也就是重定向到 404 文件。
# If request is via an image subdomain, throw error 404 for any file types other than images
RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
RewriteCond %{REQUEST_URI} !(.+)\.(jpg|jpeg|gif|png)$ [NC]
RewriteCond %{REQUEST_URI} !^/404.html$ [NC]
RewriteRule ^(.*)$ /404.html [R=404,L]