【问题标题】:nginx url rewrite (No redirection )nginx url 重写(无重定向)
【发布时间】:2016-04-29 06:34:27
【问题描述】:

您好,我想将 nginx 重写为流式

http://my-domain/Canada

http://my-domain/rates/callrates.php?c=Canada

用户将看到网址为http://my-domain/Canada 并且 php 将执行以下 url 中的页面

http://my-domain/rates/callrates.php?c=Canada

如何在 nginx.conf 中启用此功能?

server 
{
    listen 80;
    server_name  my-domain.com;
    access_log /home/www/my-domain/logs/access.log;
    error_log /home/www/my-domain/logs/error.log;
    root /home/www/my-domain/public_html;
    location / {
        index index.php login.php;
    try_files $uri $uri/ $uri.php?$query_string @extensionless-php;
    error_page  404  /404.php;      
    }
location ~* ^/(?<country>\w+)$ {
    rewrite ^ /testring/callrates.php?c=$country last;
}
 location ~ \.html$ 
 {
    if (!-f $request_filename) 
    {
        rewrite ^(.*)\.html$ $1.php last;
    }
}
location ~ .*\.php$  
    {   include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/r2wconnect.com/public_html$fastcgi_script_name;
    }
location @extensionless-php 
{
   rewrite ^(.*)$ $1.php last;
}
}

【问题讨论】:

  • 我看不出新的位置块是如何破坏.css.js 的,除非它们的网址没有扩展名,因此看起来像国家名称网址。
  • 嗨,我设法修复了它。这是由于 html 标头中的基本 url。现在一切正常。非常感谢你的时间

标签: url nginx url-rewriting rewrite


【解决方案1】:

假设您已经有一个有效的 PHP 配置...

如果您的域不执行任何其他操作,则默认操作可能是调用脚本:

location / {
    try_files $uri /rates/callrates.php?c=$uri;
}

上面有留下/的副作用。

如果你想让规则更具体,你可以用正则表达式来保护它(它也能正确提取国家名称):

location ~* ^/(?<country>\w+)$ {
    rewrite ^ /rates/callrates.php?q=$country last;
}

【讨论】:

  • 您好,我是这样的,但它为 css、js 文件提供 404 错误
  • 如果您编辑您的问题并添加您现有的server 块,我们可以提供将上述内容集成到其中的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 2013-11-05
相关资源
最近更新 更多