Error 404 when the URL contains special characters (braces, hyphens etc.)

automatix nginx-forum at nginx.us
Wed Jul 31 21:44:20 UTC 2013


In my (Zend Framework 2) application I have a catalog of cities with links
to sports pages:

page "Cities" (/catalog)
    Madrid link: website.tld/catalog/Madrid
    Berlin link: website.tld/catalog/Berlin
    London link: website.tld/catalog/London

page "Sports in London" (/catalog/London)
    Foo link: website.tld/catalog/London/Foo
    Bar (Bar) link: website.tld/catalog/London/Bar (Bar)
    Baz - Baz link: website.tld/catalog/London/Baz - Baz

The URLs are escaped and work well. But when I try to reach a page with an
unescaped special character like a brace (e.g.
"website.tld/catalog/Berlin/Jeu de Paume (Real Tennis)" instead of
"website.tld/catalog/Berlin/Jeu%20de%20Paume%20%28Real%20Tennis%29"), I get
a 404 Error.

How to fix it?

What I want to achieve is a behaviour like à la Wikipedia -- You can use
"http://en.wikipedia.org/wiki/Signal_(electrical_engineering)" or
"http://en.wikipedia.org/wiki/Signal_%28electrical_engineering%29" and will
always get the corect page.

Additional info:

System properties:

Debian 7, nginx 1.2.1, PHP 5.5.

nginx.conf:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
	worker_connections 768;
}

http {

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;
	gzip_disable "msie6";

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

ax-common-vhost:

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

    if ($host ~ ^(?<project>.+)\.(?<area>.+)\.loc$) {
        #set $project $1;			# already set
        #set $area $2;				# already set
        
        set $folder "$area/$project";
        #set $domain "$project.$area.loc";	# equal to $host
    }

    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;
  }
}

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,241421,241421#msg-241421



More information about the nginx mailing list