Welcome! Log In Create A New Profile

Advanced

Nice URLs in addition to Wordpress permalinks.

Posted by miradev 
Nice URLs in addition to Wordpress permalinks.
July 14, 2009 11:37AM
I have a pretty standard WP config.

[code]
server {
listen localhost:80;
server_name mysite.com;

access_log /var/log/nginx/myaccess.log;
error_log /var/log/nginx/myerror.log;

location / {
root /var/www/mysite.com;
index index.php index.html index.htm;

# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}


try_files /system/maintenance.html $uri $uri/ /index.php?q=$uri&$args;
}

location ~ \.php$ {
include wordpress/fastcgi_params.conf;
}
}
[/code]
(Any syntax error is just from annonymisation, the config works great)

The wordpress permalink structure is very basic: [code] /%postname% [/code]

Giving me urls that look very 'normal' (e.g. http://mysite.com/welcome).

What I'm looking to do is make http://mysite.com/products/green%20chair to work
This was my attempt:
[code]
rewrite ^/products/(.*)$ /index.php?q=/products&prodname=$1&$args;
[/code]

But this doesn't work.



Edited 1 time(s). Last edit at 07/14/2009 11:38AM by miradev.
Re: Nice URLs in addition to Wordpress permalinks.
July 14, 2009 10:52PM
If it's written with the space ( "http://mysite.com/products/green chair" ) nginx will not accept it.

--
Jim Ohlstein
Re: Nice URLs in addition to Wordpress permalinks.
July 15, 2009 03:52AM
Jim Ohlstein Wrote:
-------------------------------------------------------
> If it's written with the space (
> "http://mysite.com/products/green chair" ) nginx
> will not accept it.

I didn't know that. But even without the space, my setup still does not work (e.g. 'http://mysite.com/products/train').
The request is passed onto fcgi, as I get a WP page, but the content is empty (i.e. in the WP loop, wp_head() e.t.c are fine, but the page is unrecognised, so there is nothing in content section).

Nothing reported in the log files either (other than the request for /products/train).
Re: Nice URLs in addition to Wordpress permalinks.
July 15, 2009 09:38AM
Hmmm....

A couple of things. First, your configuration has some issues. I didn't look that closely at it last night because I was focused on that specific URI.

These parts are redundant:

[code]
# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}
[/code]
and
[code]
try_files /system/maintenance.html $uri $uri/ /index.php?q=$uri&$args;
}

[/code]

The try_files directive looks for the file as "$uri" and serves it if it's there. It's also much faster than an "if" statement. So you should be able to safely comment out that block.

Second, with your try_files directive your last entry is the fallback. So those requests are being rewritten as "/index.php?q=$uri&$args". I'm not sure which whether placing the rewrite before the try_files directive would solve that, but it's worth a try.

--
Jim Ohlstein
Re: Nice URLs in addition to Wordpress permalinks.
July 15, 2009 10:05AM
Thanks Jim.

I fiddled with the .php handling to make nginx know a bit more about the path_info, and came up with this:

[code]
location / {
root /my/root;
index index.php index.html index.htm;

rewrite ^/product/(.+)$ /index.php/product?prodname=$1 last;

try_files /system/maintenance.html $uri $uri/ /index.php?q=$uri&$args;
}

location ~ \.php($|/) {
set $script $uri;
set $path_info "";

if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}

...rest of fcgi settings..
}
[/code]

I know I've still got an if() statement for each .php hit, but I can't think of a better way at the moment.
And I believe I am losing the query_string with a /pruduct/ hit, but that is not an issue right now.
Needs a little more testing, but it seems to be serving WP pages just fine.
Re: Nice URLs in addition to Wordpress permalinks.
July 15, 2009 10:14AM
You can't necessarily eliminate *all* "if" statements. Minimizing them is good if you can. I'm glad that you seem to be on the right track.

--
Jim Ohlstein
Re: Nice URLs in addition to Wordpress permalinks.
July 18, 2009 11:29AM
I think this is how I set up wordpress with rewrites

[code]server {
listen 80;
server_name myblog.com;
index index.php;
root /path/to/wordpress;
try_files $uri /index.php;
location ~ \.php$ {
try_files $uri /index.php;
fastcgi_pass 127.0.0.1:8000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}[/code]
Re: Nice URLs in addition to Wordpress permalinks.
August 09, 2009 09:34AM
Set up WordPress on Nginx with Pretty URLs & WP Super Cache ..

http://www.guvnr.com/web/web-dev/configure-nginx-wordpress/

Hope that helps. As it suggests!, it enables WordPress Super Cache plugin as well as FURLs.

***************************************************************************************************
http://guvnr.com .. now publishing the ultimate 40-part VPS/Nginx copy/paste guide ..

Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible .. (featuring Nginx) ..
http://www.guvnr.com/web/web-dev/setup-unmanaged-vps-newbies-v-p-s-bible/
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 228
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready