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

标签 nginx apache2 zend-framework2 special-characters http-redirect

在基于 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

关于nginx - 如何替换 HTTP 请求 URI 中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252280/

相关文章:

php - ZF2 : How to make Zend\Validator\Hostname accept url strings with and without http://or https://

php - ZF2 如何将内容包装在表单字段集中?

nginx - 警告 : erroneous pipeline: no element "rtmpsink"

django - 太多 PostgreSQL 进程正在运行(使用 Django 写入数据库)

zend-framework - Ubuntu 上的 Apache 2 在某些 url 上显示 403 被禁止

mysql - 在哪里放置网络服务器根目录?

php - ZF2 应用程序范围的 var(自定义唯一请求 id)

json - Golang JSON 编码器和 nginx 代理缓冲

python - uwsgi 服务没有启动

ruby-on-rails - 尝试在升级的 Ruby/Rails/Ubuntu 机器上运行 Nginx/Phusion 乘客时出现请求处理程序错误