我希望能够重写以下网站结构,以便它既适用于主域,又适用于所有子目录。
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
,则您将需要一个单独的规则来仅匹配没有“子域”的情况在网址中:
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
。
关于php - 使用变量子目录重写 URL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59258453/