加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 当url指向一个现有的文件夹(没有尾部斜杠)时,如何重定向

发布时间:2020-12-13 16:00:30 所属栏目:PHP教程 来源:网络整理
导读:我正在使用HAProxy在80上侦听向节点服务器(端口:3000)或php服务器(4000)发送请求;我也有CSF安装有端口3000和80可用. 当我浏览http://example.com/forum/1/page.php上的一个页面时,它可以正常工作,但有时候当我不小心输入example.com/forum/1(没有尾随斜线)
我正在使用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错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读