3

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.

  • Sorry, that's not how things work here. What have you tried thus far? What worked? What didn't work as expected? Look, we want to help you, but this isn't a code-writing service. We expect that, before getting help here, that you show us what you've tried and give evidence that you've tried to solve the problem on your own. – EEAA Apr 19 '17 at 12:26
  • The syntax is almost identical! Apache RewriteRule Directive: RewriteRule Pattern Substitution [flags]. Nginx rewrite Directive rewrite regex replacement [flag];. Regex format is the same. Nginx equivalent for flag [L] is break. Convert or learn. – Esa Jokinen Apr 19 '17 at 12:37
  • @EEAA I've added an example on what I've tried, is that suitable? – Luke Apr 19 '17 at 12:58
  • You should also provide examples of the URLs you're trying to operate on - before and after rewrite. The more information you can give, the better. You need to help us first, before we can help you. – EEAA Apr 19 '17 at 13:11
  • @EEAA Just added more details – Luke Apr 19 '17 at 13:18
3

The location block is the usual place for your rewrite directives, so you got it right.

The only thing visibly missing from your configuration is the leading / in your regular expressions:

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;
 }
  • The trailing ? on the 2nd rule pattern (ie. ^/([A-Za-z0-9-]+)?$) would seem to be unnecessary, given the first rule? (Unless there should be a trailing slash that is omitted from the original rule?) – MrWhite Apr 19 '17 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.