【问题标题】:How can keep my existing rewrite rules working with a WordPress install?如何让我现有的重​​写规则与 WordPress 安装一起工作?
【发布时间】:2023-03-05 08:27:02
【问题描述】:

我有一个非常简单的网站,它有一些静态页面,还有一些使用重写规则来提取数据的动态页面。例如:

RewriteRule ^phone-directory/([^/\.]+).htm$ number.php?for=$1 [L]

因此,像 phone-directory/google.htm 这样的任何网址总是会在查询字符串上加载 number.phpgoogle

我已经为 CMS 功能安装了 WordPress,但我仍然需要我的目录同时工作。我还希望目录使用 WordPress 主题,以便在站点范围内更改样式!

所以我在我的 WP 主题目录中设置了一个自定义模板,并在 word press 中设置了一个名为“company-listing”的“页面”。然后我将以下规则添加到我的 htaccess 文件的顶部:

RewriteRule ^phone-directory/([^/\.]+).htm$ index.php?p=23 [L]

p=23 指的是我的自定义页面“公司列表”的位置

该重写规则有效并将我带到“公司列表”,但这就是问题所在,URL 重写为 domain.com/company-listing

我不希望它这样做。我认为我的重写规则被触发,然后当 index.php?p=23 被击中时,默认的 WordPress 重写规则触发器。我已经尝试了以下排除方法来阻止这种情况的发生:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(phone-directory/.*)$
RewriteCond %{REQUEST_URI} !^/(index.php?p=23)$
RewriteCond %{REQUEST_URI} !^/(company-listing/.*)$
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

有没有办法解决这个问题?要在 WordPress 中使用自定义模板并重写为我选择的 url?总结:

我想要:

domain.com/phone-directory/google.htm

加载:

domain.com/index.php?p=23(这是 wordpress 中的“页面”)

【问题讨论】:

    标签: wordpress .htaccess url-rewriting rewrite


    【解决方案1】:

    你只需要在你的 wordpress 规则之前有你的规则:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^phone-directory/([^/\.]+).htm$ number.php?for=$1 [L]
    RewriteRule ^phone-directory/google.htm$ index.php?p=23 [L]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    如果前面的规则之一匹配,只要number.php 存在,wordpress 路由规则的!-f 条件就会失败。

    【讨论】:

    • 嗨乔恩。问题是,当 index.php?p=23 被击中时……那是进一步匹配 wordpress 规则的地方。这就是为什么 index.php?p=23 中的 url 最终出现在 /company-listing 上,因为它是一个 wordpress 页面。
    • @JamesWilson /index.php?p=23 将匹配 RewriteRule ^index\.php$ - [L],这将完全停止重写。如果浏览器被重定向,那是因为 wordpress 正在这样做。
    • 同意。我已经设法解决了。我已经将我的重写设置为访问一个名为“directory-listing.php”的新页面,并且在该页面上有一些代码执行 index.php?p=23 的 file_get_contents - 所以它吸收了正确的内容,我动态改变它,没有任何进一步的重定向!
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多