Welcome! Log In Create A New Profile

Advanced

Cannot get simple rewrite rules working in subdirectory

Posted by Kevin C 
Cannot get simple rewrite rules working in subdirectory
March 09, 2013 09:50PM
We are trying to move a photopost installation from Apache to nginx and are having difficulties with the rewrite rules. Photopost provides some VERY basic rewrite rules for Apache and we have tried to convert them to nginx but apparently have failed miserably. Here are the .htaccess rules:

RewriteEngine on
Options +FollowSymLinks

#RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
#RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/p([0-9]+)-(.*)-page([0-9+]).html$ showproduct.php?product=$2&cpage=$4 [L]
RewriteRule ^(.*)/p([0-9]+)-(.*).html$ showproduct.php?product=$2 [L]
RewriteRule ^(.*)/p([0-9]+).html$ showproduct.php?product=$2 [L]

RewriteRule ^g([0-9]+)-(.*)-page([0-9]+).html$ showcat.php?cat=$1&page=$3 [L]
RewriteRule ^g([0-9]+)-(.*).html$ showcat.php?cat=$1 [L]

RewriteRule ^(.*)/index([0-9]+)-([0-9]+).html$ index.php?cat=$2&page=$3 [L]
RewriteRule ^(.*)/index([0-9]+).html$ index.php?cat=$2 [L]

RewriteRule ^m([0-9]+)-(.*)-protype([0-9]+).html$ member.php?uid=$1&protype=$3 [L]
RewriteRule ^m([0-9]+)-(.*).html$ member.php?uid=$1 [L]

RewriteRule ^board.html$ board.php [L]
RewriteRule ^b([0-9]+)-(.*).html$ board.php?msg=$1 [L]

RewriteRule ^u([0-9]+)-(.*)-page([0-9]+).html$ showcat.php?ppuser=$1&page=$3 [L]
RewriteRule ^u([0-9]+)-(.*).html$ showcat.php?ppuser=$1 [L]

RewriteRule ^s([0-9]+)-(.*)-page([0-9]+).html$ showmembers.php?cat=$1&page=$3 [L]
RewriteRule ^s([0-9]+)-(.*).html$ showmembers.php?cat=$1 [L]



This is what we converted them to for nginx:

location /classifieds/ {

rewrite ^/classifieds/(.*)/p([0-9]+)-(.*)-page([0-9+]).html$ /classifieds/showproduct.php?product=$2&cpage=$4 permanent;
rewrite ^/classifieds/(.*)/p([0-9]+)-(.*).html$ /classifieds/showproduct.php?product=$2 permanent;
rewrite ^/classifieds/(.*)/p([0-9]+).html$ /classifieds/showproduct.php?product=$2 permanent;

rewrite ^/classifieds/g([0-9]+)-(.*)-page([0-9]+).html$ /classifieds/showcat.php?cat=$1&page=$3 permanent;
rewrite ^/classifieds/g([0-9]+)-(.*).html$ /classifieds/showcat.php?cat=$1 permanent;

rewrite ^/classifieds/(.*)/index([0-9]+)-([0-9]+).html$ /classifieds/index.php?cat=$2&page=$3 permanent;
rewrite ^/classifieds/(.*)/index([0-9]+).html$ /classifieds/index.php?cat=$2 permanent;

rewrite ^/classifieds/m([0-9]+)-(.*)-protype([0-9]+).html$ /classifieds/member.php?uid=$1&protype=$3 permanent;
rewrite ^/classifieds/m([0-9]+)-(.*).html$ /classifieds/member.php?uid=$1 permanent;

rewrite ^/classifieds/board.html$ /classifieds/board.php permanent;
rewrite ^/classifieds/b([0-9]+)-(.*).html$ /classifieds/board.php?msg=$1 permanent;

rewrite ^/classifieds/u([0-9]+)-(.*)-page([0-9]+).html$ /classifieds/showcat.php?ppuser=$1&page=$3 permanent;
rewrite ^/classifieds/u([0-9]+)-(.*).html$ /classifieds/showcat.php?ppuser=$1 permanent;

rewrite ^/classifieds/s([0-9]+)-(.*)-page([0-9]+).html$ /classifieds/showmembers.php?cat=$1&page=$3 permanent;
rewrite ^/classifieds/s([0-9]+)-(.*).html$ /classifieds/showmembers.php?cat=$1 permanent;
}

However, these rules are not working for us and only result in a 404. Can anyone shed any light on where we're going wrong here?
Re: Cannot get simple rewrite rules working in subdirectory
May 06, 2013 08:06AM
Hello,

Remove your entire /classifields location and try location-based capture without any rewrite's:

location ~ ^.*/p(?<product>\d+)(-.*-page(?<page>\d+)|.*)\.html$
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/showproduct.php;
fastcgi_param QUERY_STRING product=$product&cpage=$page;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^/g(?<cat>\d+)-(.*-page(?<page>\d+)|.*)\.html$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/showcat.php;
fastcgi_param QUERY_STRING cat=$cat&page=$page;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^.*/index(?<cat>\d+)(-(?<page>\d+)|.*)\.html$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING cat=$cat&page=$page;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^/m(?<uid>\d+)-(.*-protype(?<protype>\d+)|.*)\.html$
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/member.php;
fastcgi_param QUERY_STRING uid=$uid&protype=$protype;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^/(board\.html|b(?<msg>\d+)-.*\.html)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/board.php;
fastcgi_param QUERY_STRING msg=$msg;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^/u(?<user>\d+)-(.*-page(?<page>\d+)|.*)\.html$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/showcat.php;
fastcgi_param QUERY_STRING ppuser=$user&page=$page;
fastcgi_pass ...; # connect to to php-fpm socket
}

location ~ ^/s(?<cat>\d+)-(.*-page(?<page>\d+)|.*)\.html$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/showmembers.php;
fastcgi_param QUERY_STRING cat=$cat&page=$page;
fastcgi_pass ...; # connect to to php-fpm socket
}

Andrejs
loco (at) andrews.lv
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 293
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