微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 当url指向一个现有的文件夹(没有尾部斜杠)时,如何重定向到404. HAproxy或mod-rewrite相关

我正在使用HAProxy在80上侦听向节点服务器(端口:3000)或PHP服务器(4000)发送请求;我也有CSF安装有端口3000和80可用.

当我浏览http://example.com/forum/1/page.PHP上的一个页面时,它可以正常工作,但有时候当我不小心输入example.com/forum/1(没有尾随斜线)时,它会不断加载,最终导致到错误页面ERR_CONNECTION_TIMED_OUT.地址栏显示已将其重定向到http://example.com:4000/forum/1/.

由于我没有打开4000端口,所以当我继续多次访问example.com/forum/1时,它将会被CSF触发一个块.我的问题是,我可以将所有指向实际文件夹example.com/forum/1(无拖尾斜杠)的请求重定向到404页面吗?我已经尝试添加一个重写规则,为每个请求附加一个斜杠,但这会破坏我的所有相对路径(请参阅我在post的问题).

所以我想知道的是,为什么我从http://example.com/forum/1重定向到http://example.com:4000/forum/1/?是否由HAproxy引起?

一些HAproxy配置:

frontend all 0.0.0.0:80
    timeout client 1h
    # use apache2 as default webserver for incoming traffic
    default_backend apache2

backend apache2
    balance roundrobin

    option forwardfor
    server apache2 myIpAddress:4000 weight 1 maxconn 1024 check 

    # server must be contacted within 5 seconds
    timeout connect 5s
    # all headers must arrive within 3 seconds
    timeout http-request 3s
    # server must respond within 25 seconds. should equal client timeout
    timeout server 25s

这是我的重写规则:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$[NC]
RewriteCond %{REQUEST_FILENAME}.PHP -f
RewriteRule !.*\.PHP$%{REQUEST_FILENAME}.PHP [QSA,L]

RewriteCond %{THE_REQUEST} /index\.PHP [NC]
RewriteRule ^([^\.]+)$$1.PHP [NC,L]
由于/ forum / 1是一个有效的物理目录,由于此设置,您将被重定向到/ forum / 1 /
DirectorySlash On

它被称为mod_dir的模块用于在目录之后添加尾部斜杠(如果缺少).

您当然可以使用以下命令关闭此标志:

DirectorySlash Off

但是be aware of security implications.

为了更安全,您还需要:

Options -Indexes

关闭目录列表.

安全警告(从链接手册复制)

关闭尾部斜线重定向可能会导致信息泄露.考虑mod_autoindex处于活动状态(Options Indexes),DirectoryIndex设置为有效资源(例如,index.html),并且没有为该URL定义其他特殊处理程序.在这种情况下,带有尾部斜线的请求将显示index.html文件.但是没有拖尾的请求将列出目录内容.

更新:回答这一部分问题:

My question is,can I redirect all the requests that are pointing to an actual folder example.com/forum/1 (no trailing slash) to an 404 page?

您可以在/forum/1/.htaccess中使用此代码

DirectorySlash On
RewriteEngine On

RewriteRule ^$- [L,R=404]

这将强制一个尾部的斜线,以便重写规则可以用来发送404错误.

原文地址:https://www.jb51.cc/php/132956.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐