I'm having trouble converting some .htaccess rules over to an nginx server.
RewriteRule ^$ index.php [L]
RewriteRule ^([A-Za-z0-9-]+)?$ index.php?section=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?section=$1&go=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?section=$1&go=$2&action=$3 [L]
Would someone be able to assist in converting and explain how these regular expressions would convert over to nginx?
I'm unsure of the syntax, as per the nginx documentation I've tried the following:
server {
rewrite ^([A-Za-z0-9-]+)?$ index.php?section=$1&go=$2;
}
I've also tried within a root location block as shown below. I'm not sure how the try_files
affects this though.
location / {
rewrite ^$ /index.php break;
rewrite ^([A-Za-z0-9-]+)?$ /index.php?section=$1 break;
rewrite ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /index.php?section=$1&go=$2 break;
rewrite ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /index.php?section=$1&go=$2&action=$3 break;
rewrite ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /index.php?section=$1&go=$2&action=$3&id=$4 break;
try_files $uri $uri/ /index.php?$args;
}
That doesn't seem to work though. Does this need to be placed within a location block first?
The URLs I'm testing are the following. Hostname has been removed as I'm not permitted to share it.
http://www.example.com/entry
http://www.example.com/volunteers
http://www.example.com/contact
Basically the page is loading as if index.php is being accessed and the sections are not loading if that makes sense. Nothing seems to be passed into the index.php script.
RewriteRule Pattern Substitution [flags]
. Nginx rewrite Directiverewrite regex replacement [flag];
. Regex format is the same. Nginx equivalent for flag[L]
isbreak
. Convert or learn. – Esa Jokinen Apr 19 '17 at 12:37