symfony - Symfony 重写 nginx
问题描述
我正在使用 Symfony 3.4 运行,并且我已经将我的 nginx 配置为直接使用http://example.org/my-route而不是http://example.org/app.php/my-route
server {
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
# bla bla bla
}
location ~ ^/app\.php(/|$) {
# bla bla bla
}
}
但现在我想将访问我网站的用户重定向到http://example.org/app.php/my-route到http://example.org/my-route
我尝试创建一个新的重写,但我得到了循环重定向
rewrite ^/app.php(.*) /$1 permanent;
我应该只在我的 app.php 中添加一个 header() 吗?
if (strpos($request->getUri(), $request->getHost().'/app.php') !== false) {
$uri = str_replace($request->getHost().'/app.php', $request->getHost(), $request->getUri());
header('Location: ' . $uri, true, 301);
}
解决方案
为避免循环,您需要识别何时/app.php
是原始请求,而不是try_files
语句生成的相同 URI。
原始请求可以在$request_uri
变量中找到,可以使用if
块进行测试。
例如:
if ($request_uri ~ ^/app.php(.+)$) { return 301 $1; }
请参阅此使用注意事项if
。
推荐阅读
- java - 如何使 EditText 不允许输入任何数字?
- javascript - 我想单击父组件以在反应中呈现子组件
- apache-flink - 如何在 apache flink 中实现对账系统
- r - AICc():logLik 出错,不能应用于“逻辑”类的对象
- json - 调用 API 并将数据存储到 SQL Server 表中
- flutter - 将错误响应转换为特定模型 - 斩波器颤振
- python - 显示 tkinter 字体
- r - R 中的 httr 请求在特定 Internet 连接上无法在 Ubuntu 18.04 LTS 上运行(但在 Windows 10 上运行)
- forms - Symfony FileType:class 在尝试更新时不起作用
- dataframe - pySpark 映射多列