Welcome! Log In Create A New Profile

Advanced

Nginx Rewrite rules for clean URLs

Posted by ffernic 
Nginx Rewrite rules for clean URLs
October 15, 2009 05:21PM
I want to write nginx rewrite rules for clean URLs.

Everytime the user hits http://domain.com/abc/12/16/abc-def-ghi I need to execute domain.com/abc.php?a=12&b=16&c=abc-def-ghi.

Now my regex is right as per rubular: ^\/abc\/(\d+)\/(\d+)\/(\w+\S+)$
http://rubular.com/regexes/11063

and rule is

if (!-e $request_filename) {
rewrite ^\/abc\/(\d+)\/(\d+)\/(\w+\S+)$ abc.php?a=$1&b=$2&c=$3 last;

}

But it is giving "No input File specified". I cant find what the problem is?
Re: Nginx Rewrite rules for clean URLs
October 15, 2009 07:43PM
A simpler regex can be seen at http://rubular.com/regexes/11064. Also, your "abc.php" needs a forward slash. You can try:

[code]
location / {
if (!-e $request_filename) {
rewrite ^/abc\/(.*)\/(.*)\/(.*)$ /abc.php?a=$1&b=$2&c=$3 last;
}
}
[/code]

If this is the only rewrite and you are using a recent version of nginx you might use a "try_files" directive which is far more efficient than an "if" statement. Requires nginx 0.6.36+, 0.7.27+, 0.8.x.

Something like:

[code]
# this will serve the file if it exists or the index file if the uri is a directory
# if file does not exist it will pass internally to your app

location / {
try_files $uri $uri/ @app;
...
}

location @app {
rewrite ^/abc\/(.*)\/(.*)\/(.*)$ /abc.php?a=$1&b=$2&c=$3 last;
...
}
[/code]

More info on try_files at http://wiki.nginx.org/NginxHttpMainModule#try_files.

--
Jim Ohlstein



Edited 1 time(s). Last edit at 10/15/2009 07:43PM by Jim Ohlstein.
Re: Nginx Rewrite rules for clean URLs
October 16, 2009 01:56AM
Thanks Jim. How silly of me to forget the /. The rules work fine and the page is rendered but I get a garbled CSS. My top navigation bar appears as a three line bar at the leftmost side. So I guess although PHP is working fine, the CSS files are having a problem.
Is it because I am missing in adding something or the way I write my CSS? With the original URL (w/o) the rule the CSS is rendered properly and I see the Nav Bar as it should be seen.
Re: Nginx Rewrite rules for clean URLs
October 16, 2009 06:48AM
Please post the full configuration file.

--
Jim Ohlstein
Re: Nginx Rewrite rules for clean URLs
October 16, 2009 09:04AM
Nginx.conf

server {
listen 80;
server_name test.domain.com;
index index.php;
#charset koi8-r;

#access_log logs/host.access.log main;
location / {
root test.domain.com/html;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^\/post\/(\d+)\/(\d+)\/(\w+\S+)$ /post.php?a=$1&b=$2&c=$3 last;
}

}
location ~ \.php$ {
root test.domain.com/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/test.domain.com/html$fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}
}
Re: Nginx Rewrite rules for clean URLs
October 16, 2009 10:04AM
I'm not sure why this would be happening. The configuration looks fairly generic.

You can try adding something like:

[code]
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
expires max;
}
[/code]

in order to force nginx to serve those files directly.

You might also try putting the rewrite directive outside a location block.

I'm not sure if either will work but worth a try.

--
Jim Ohlstein
Re: Nginx Rewrite rules for clean URLs
October 16, 2009 01:56PM
Thanks Jim. I am trying a lot of stuff. Will let you know if something works or I come across some other problem.
Re: Nginx Rewrite rules for clean URLs
October 20, 2009 09:22AM
This looks like an impossible problem to solve. Anyplace else I can address this problem.
Following are the two test URLs. The clean URL is converted to the .php url by the above rules.
But the CSS still gives a problem.

http://test.redanyway.com/post/805/1239860588/crazy-evening-crazier-friends

http://test.redanyway.com/post.php?id=805&time=1239860588&slug=crazy-evening-crazier-friends
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 314
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready