0

I have a WordPress site running on Pantheon, which uses nginx. Rewrite rules in the .htaccess files are ignored, so they need to be placed in wp-config.php instead. My regex skills are more or less nonexistent, and I need to convert these rewrite rules to PHP:

rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

Can anyone point me in the right direction?

wp78de
  • 18,207
  • 7
  • 43
  • 71
joakland
  • 145
  • 8
  • Aren't you still going to be using the regex in PHP? – chris85 Apr 25 '18 at 18:08
  • Yes, but there are minor syntactical differences for regex in PHP. Also, being only moderately familiar with mod_rewrite rules, I can see there are aspects to these rules that also need conversion to PHP, besides just the regex. – joakland Apr 25 '18 at 18:13
  • 1
    The only difference I think you'll need is the delimiters. They both are using PCRE so it should perform the same. You can set the `$_GET`s in the code and then include the `index.php` so it loads the expected content..or you could use a `header` if it is suppose to redirect. – chris85 Apr 25 '18 at 18:31
  • Thanks! What about the "last" after each rewrite rule? How should this get handled in php? – joakland Apr 25 '18 at 19:15
  • 1
    I've never seen the `last` like that before. I've seen `[L]` and if that is the same you should be able to just use a series of conditionals for this. `if(preg_match)..}elseif(preg_match){...`` – chris85 Apr 25 '18 at 19:37
  • I have also created a new pantheon tag for better discoverability and attached it to a number of related question. – wp78de Apr 28 '18 at 03:17
  • @joakland was the answer helpful to you? – wp78de Apr 28 '18 at 03:17

2 Answers2

0

Some comments.

First regarding the last flag, straight from the Apache documentation on rewrite:

The [L|last] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

Here are some more details on last.

Therefore, the pattern you would have to implement should look like a switch statement or a series of if-elseifs.

However, the Nginx wiki says using "if" is evil. It is suggested useing try_files instead.

See also/Further references:

wp78de
  • 18,207
  • 7
  • 43
  • 71
0

This is what I use. Uses curl to proxy to a different URL path. Works with Drupal and Wordpress. https://github.com/mpaladino/pantheon-htaccess-rewrites