简体   繁体   English

将 .htaccess 重写规则转换为 PHP

[英]Convert .htaccess rewrite rule to PHP

I have a WordPress site running on Pantheon, which uses nginx.我有一个在 Pantheon 上运行的 WordPress 站点,它使用 nginx。 Rewrite rules in the .htaccess files are ignored, so they need to be placed in wp-config.php instead. .htaccess 文件中的重写规则被忽略,因此需要将它们放在 wp-config.php 中。 My regex skills are more or less nonexistent, and I need to convert these rewrite rules to PHP:我的正则表达式技能或多或少不存在,我需要将这些重写规则转换为 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?任何人都可以指出我正确的方向吗?

Some comments.一些评论。

First regarding the last flag, straight from the Apache documentation on rewrite :首先关于last标志,直接来自 关于重写Apache 文档

The [L|last] flag causes mod_rewrite to stop processing the rule set. [L|last]标志导致mod_rewrite停止处理规则集。 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.这对应于 Perl 中的最后一个命令,或 C 中的 break 命令。使用此标志来指示应立即应用当前规则而不考虑其他规则。

Here are some more details on last .这里有一些关于last更多细节

Therefore, the pattern you would have to implement should look like a switch statement or a series of if-elseif s.因此,您必须实现的模式应该看起来像一个switch语句或一系列if-elseif

However, the Nginx wiki says using "if" is evil .但是,Nginx wiki 说使用“if”是邪恶的 It is suggested useing try_files instead.建议改用try_files

See also/Further references:另见/进一步参考:

This is what I use.这就是我使用的。 Uses curl to proxy to a different URL path.使用 curl 代理到不同的 URL 路径。 Works with Drupal and Wordpress.适用于 Drupal 和 Wordpress。 https://github.com/mpaladino/pantheon-htaccess-rewrites https://github.com/mpaladino/pantheon-htaccess-rewrites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM