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

标签 php wordpress .htaccess url-rewriting

我希望能够重写以下网站结构,以便它既适用于主域,又适用于所有子目录。

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/

相关文章:

php - 如何从mysql中的多个表中按日期顺序获取数据?

php - 仅显示数组中包含特定单词的值

css - 删除+添加新产品类别链接

php - Wordpress 使用插件创建前端页面

php - 在 HTTP WordPress 站点中加载外部 HTTPS 脚本

http - 错误文档和重写 URL

php - INNER JOIN 语句中的错误

带有 PayPal 新 header 要求的 PHP Curl 帖子

.htaccess - API在url中配置参数

php - htaccess 重定向 url 到页面