提问人:WannabePuppetMaster 提问时间:1/8/2023 最后编辑:RavinderSingh13WannabePuppetMaster 更新时间:1/12/2023 访问量:105
Wordpress 的重写 API 是否与 Root 以外的 RewriteBase 中断?
Does Wordpress' Rewrite API Break with RewriteBase other than Root?
问:
我试图让 Wordpress 使用基本的重写规则度过了一段可怕的时光。我在插件目录中有一个 php 页面,它需要接收一个 ID 号,以便它可以查找相关记录。我希望用户访问 url example.com/redirect/{redirectid},它应该提供在 example.com/wp-content/plugins/myplugin/redirect/index.php?redirectid=$1 找到的页面,但这永远不起作用。
我尝试通过重写 API 添加重定向规则。该规则在创建规则时似乎忽略了 RewriteBase。这是 Windows PC 上的 Bitnami Wordpress 安装,它将 WP 站点托管在名为 /wordpress/ 而不是根目录的子目录中,因此 RewriteBase 是 /wordpress/ 而不是 /。
当我尝试添加重写规则时,如下所示,它只将 RewriteBase 预置到第二部分。
add_rewrite_rule( 'redirect/([0-9]+)/?', 'wp-content/plugins/myplugin/redirect/index.php?redirectid=$matches[1]', 'top' );
add_rewrite_tag( '%redirect%', '(.*)' );
flush_rewrite_rules();
这会导致将以下重写规则添加到 .htaccess 文件中:
RewriteRule ^redirect/([0-9]+)/? /wordpress/wp-content/plugins/myplugin/redirect/index.php?urlid=$matches[1] [QSA,L]
尽管有此规则,但像 example.com/redirect/1/ 这样的 URL 会导致 Wordpress 生成 404 错误页面。部分原因可能是由于页面使用了与 /pagename/ 类似的 URL 结构,但这与 /pagename/id/ 不同,因此应该没问题。我尝试将规则复制并粘贴到 .htaccess 文件中的 Wordpress 上方和下方,但结果是一样的。如果我手动编辑规则的第一部分说^wordpress/redirect/([0-9]+)/?
我不知道如何检查Wordpress中当前有哪些查询变量。rewrite 标签函数不会向 .htaccess 文件添加任何内容,我似乎在任何地方都找不到存储在数据库中的查询变量。我尝试了以下方法,但它只生成了一个空数组:
global $wp_query;
var_dump($wp_query->query_vars);
因为这一切都在插件的激活功能中进行,所以我需要一个完全基于 Wordpress 代码的解决方案,以避免必须指示用户直接自己更改他们的 .htaccess 文件或向用户提供长 URL 到他们的帖子中插件。它是一个 URL 伪装插件,所以我宁愿向用户显示一个看起来比插件目录中的 php 文件位置更干净的 URL,它只不过是根据 id 在数据库中查找 URL 并重定向用户。
另外,是否可以阻止规则的 QSA 部分由 Wordpress 自动生成?其他人告诉我要摆脱 QSA,但我不知道如何在不手动编辑 .htaccess 文件的情况下进行操作。
根据 Regex101 的说法,问题是 Wordpress 自动预置的克拉字符。如果我测试这个正则表达式时没有预置字符,只要斜杠被转义 https://regex101.com/r/3vVMV7/1 它就可以正常工作,这表明出了问题,因为我看到的 API 的每个示例都没有转义斜杠,并且没有提供阻止克拉被预置的方法。
更新:此问题是由Bitnami引起的。我在他们的 Github 支持论坛上发布了一个问题。他们的回答是,“默认情况下,我们的安装会禁用 Apache 软件基金会建议的 .htaccess 文件。安装时在这些文件中找到的所有配置都将移动到 /opt/bitnami/apache2/conf/vhosts/htaccess 文件夹下的单独 wordpress-htaccess.conf 文件中。您可以在那里添加插件所需的配置,或启用 .htaccess 文件。请在下面链接的指南中找到有关此内容的更多信息”
我的回应是让他们知道他们的立场有多离谱,并询问如何使用 Wordpress 重写 API 编辑他们的特殊文件,因为我怀疑这是否可能。建议:不要使用 Bitnami。
答:
Just enable basic rewrite rules in your .htaccess file as so:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
评论
STOP USING BITNAMI!
When I stopped using Bitnami, I was able to find a rewrite rule that works. It required me to edit a live site on a remote server due to my development environment using Bitnami. Bitnami responded to my support questions with an admission that they disable .htaccess by default. They provided advice for how to enable it but the advice did not work.
WITHOUT BITNAMI you can add a rewrite rule and see it work as follows:
add_rewrite_rule( "redirect/([0-9]+)/" , 'wp-content/plugins/myplugin/redirect/index.php?urlid=$1','top');
flush_rewrite_rules();
The above must go in your activation function for your plugin.
New Problem: Sometimes the .htaccess file gets overwritten by Wordpress and other plugins requiring that the plugin be deactivated before being reactivated to recreate the rules. For instance, when I changed the permalink structure for posts the .htaccess file was regenerated without the rule.
评论
/wordpress
.htaccess
/wordpress