nginx - 如何替换 HTTP 请求 URI 中的特殊字符?

在基于 Zend Framework 2 的网站(nginx 上的测试环境和 Apache 上的实时环境)上有一个类别“类(class)”,其页面具有如下 URI:

domain.tld/courses/123-Name of course that can contain ®, €, (, ), and other special chars

类(class)名称来自数据库,并针对内部链接进行了 URL 编码:

domain.tld/courses/123-Name%20of%20course%20that%20can%20contain%20%C2%AE%2C%20%E2%82%AC%2C%20%C3%A4%2C%20(%2C%20)%2C%20and%20other%20special%20chars

它工作正常,但是当我尝试使用没有编码的特殊字符访问页面时发生 404 错误。

使用空格字符的网站示例是维基百科。你可以使用

http://en.wikipedia.org/wiki/Signal_(electrical_engineering)

http://en.wikipedia.org/wiki/Signal_%28electrical_engineering%29

并且总是得到你想要的页面。

有人知道如何实现这种行为(“à la Wikipedia”)吗? (也许使用 .htaccess 规则进行 HTTP 重定向?)


更新:

/etc/nginx/ax-common-vhost

server {
    listen   80;
    server_name
        foo.loc
        bar.loc
        baz.loc
    ;

    if ($host ~ ^(?<project>.+)\.(?<area>.+)\.loc$) {
        set $folder "$area/$project";
    }

    access_log /var/log/nginx/$area/$project.access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_min_length 1000;
    gzip_types text/plain text/xml application/xml;

    client_max_body_size 25m;

    root /var/www/$folder/public/;

    try_files $uri $uri/ /index.php?$args;
    index index.html index.php;

    location / {
        index index.html index.php;
    sendfile off;
    }

    location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
        deny all;
    }

    location ~ \.htaccess {
        deny all;
    }

    if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
    }

    location ~ \.php$ {
      fastcgi_cache        off;
      #fastcgi_pass        127.0.0.1:9001;
      fastcgi_pass         unix:/var/run/php5-fpm.sock;
      fastcgi_read_timeout 6000;
      fastcgi_index        index.php;
      include              fastcgi_params;
      fastcgi_param        SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param        APPLICATION_ENV development;
      fastcgi_param        HTTPS $https;
  }
}

最佳答案

您可以通过在 .htaccess 文件中设置正确的重写规则来实现预期的 URL 重写行为。

我建议你看看 rewriteflags ,特别是 B flag

https://stackoverflow.com/questions/17252280/

相关文章:

xaml - Telerik RadMaskedNumericInput 掩码

php - 强制文件下载代码在本地主机上工作,但在 php 中的实际服务器上不工作

python - Selenium:FirefoxProfile 异常无法加载配置文件

ms-access - 如何在 vba(access) 中将变量设置为我的表单列表框之一?

android-layout - 在滑动选项卡中水平滚动 : android

ruby-on-rails - 如何加入 Rails 中的多对多表

macos - 运行 make 时如何修复 ranlib/libtool 错误

ruby-on-rails - rails 和 Heroku : Running a script

ajax - 我可以添加 Access-Control-Allow-Origin : * to th

sql-server - 在 SQL Server 中的数字字段上没有聚合的数据透视