使用变量子目录重写 URL 不起作用



我希望能够重写以下网站结构,使其同时适用于主域,然后适用于所有子目录。

domain.ltd
|    | wp-content
|       | themes
|           | theme01
|               | core
|                   | web.php   # http://domain.ltd/wp-content/themes/theme01/core/web.php
|
|- subsite1
|   | wp-content/themes/theme02/core
|       | web.php               # http://domain.ltd/subsite1/wp-content/themes/theme02/core/web.php
|
|- subsite2
|   | wp-content/themes/theme03/core
|       | web.php               # http://domain.ltd/subsite2/wp-content/themes/theme03/core/web.php

我有以下.htaccess规则:

RewriteRule ^([_0-9a-zA-Z-])/ws/(.*) /wp-content/themes/$1/core/$2

但是我不知道为什么它不匹配。我尝试使用 https://htaccess.madewithlove.be 测试器,但找不到正确的模式。

从本质上讲,我希望URL是:

http://domain.ltd/ws/web.php
http://domain.ltd/subsite1/ws/web.php
http://domain.ltd/subsite2/ws/web.php

我怀疑您的问题缺少一些细节,或者存在错误,因为它没有完全解释您希望如何重定向 URL 的所有变体。

如果您想像您所说的那样将"主域"重定向到默认每次theme01,则需要一个单独的规则来仅匹配 URL 中没有"子域"的情况:

RewriteRule ^/?ws/([w.-]+)$ /wp-content/themes/theme01/core/$1 [L]

这会将http://domain.ltd/ws/web.php重定向到/wp-content/themes/theme01/core/web.php,并且您有效地将theme01视为默认主题。

然后,您需要第二条规则来重定向所有包含"子域"的变体,并将其用作主题名称(这似乎是您想要做的(:

RewriteRule ^/?([w-]+)/ws/([w.-]+)$ /wp-content/themes/$1/core/$2 [L]

这会http://domain.ltd/subdomain2/ws/web.php重定向到/wp-content/themes/subdomain2/core/web.php

如果您不介意,一个简单的解决方案就是简单地创建一个 ws 文件夹并让所有文件都在里面。此外,在根文件夹中创建重定向规则到您的主网站

domain.ltd/ws
|    | wp-content
|       | themes
|           | theme01
|               | core
|                   | web.php   # http://domain.ltd/wp-content/themes/theme01/core/web.php
|
|- subsite1
|   | wp-content/themes/theme02/core
|       | web.php               # http://domain.ltd/subsite1/wp-content/themes/theme02/core/web.php
|
|- subsite2
|   | wp-content/themes/theme03/core
|       | web.php               # http://domain.ltd/subsite2/wp-content/themes/theme03/core/web.php

最新更新