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.

6
  • 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. Commented Apr 19, 2017 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. Commented Apr 19, 2017 at 12:37
  • @EEAA I've added an example on what I've tried, is that suitable? Commented Apr 19, 2017 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. Commented Apr 19, 2017 at 13:11
  • @EEAA Just added more details Commented Apr 19, 2017 at 13:18

1 Answer 1

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; } 
1
  • 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?) Commented Apr 19, 2017 at 14:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.