【问题标题】:Rewriting URL with variable subdirectory not working使用变量子目录重写 URL 不起作用
【发布时间】:2019-12-10 00:11:28
【问题描述】:

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

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 测试仪,但找不到正确的模式。

基本上我希望网址是:

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

【问题讨论】:

标签: php wordpress .htaccess url-rewriting


【解决方案1】:

我怀疑您的问题缺少一些细节,或者存在错误,因为它没有完全解释您希望如何重定向 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

【讨论】:

    【解决方案2】:

    如果您不介意,一个简单的解决方案就是创建一个 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2012-01-25
      相关资源
      最近更新 更多