gpt4 book ai didi

php - 如何使用 .htaccess 将静态 URL 重定向到动态 URL(在 Wordpress 站点上)

转载 作者:搜寻专家 更新时间:2023-10-31 21:31:06 24 4
gpt4 key购买 nike

我接管了以前的站点/域,并使用 Wordpress 建立了一个新站点。 WP 安装将 URL 重写为静态 URL,正如您所期望的那样。

同时我想保留以前的页面,因为它们有传入链接。我对通过 301 将它们转到"new"页面不感兴趣。

旧的 URL 结构是 /index.php?id=123,我怀疑这是导致 WP .htaccess 文件出现问题的原因。作为引用,这是它的样子:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

我试过添加以下内容:

RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]

不起作用。只需重定向到 site.com/?id=123 并显示首页。

我应该补充一点,我计划将这些新页面添加为 123.html、321.html 等格式的常规静态 HTML 文件。

我如何使用 .htaccess 使其与 WP 安装以及 WP 放入 .htaccess 文件的内容一起工作?

澄清一下:

我想让我的 123.html 静态 HTML 页面成为 index.php?id=123。当您访问 index.php?id=123 时,它应该调出 123.html,但显示 index.php?id=123地址栏。如果您访问 123.html,它应该 301 到 index.php?id=123

最佳答案

要将带有查询字符串的 URL 映射到实际文件,您需要使用 RewriteCond 来匹配查询字符串本身(因为 RewriteRule 不会):

按照这些思路应该可以做到这一点:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]

# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

这将首先检查您是否收到了带有类似 id=X 的查询字符串的 index.php 请求。

然后它会检查名为X.html 的文件是否确实存在;我不是 100% 高兴必须使用更多系统饥饿的子请求文件检查 -F 而不是标准 -f 但我看不到解决方法在本例中为 .htaccess。

如果 X.html 确实存在,它将获取该文件,同时将 URL 保留为 index.php?id=X

但是,如果该文件不存在,它将返回到标准 WordPress 无文件无目录路由到 index.php

我不是 WordPress 专家,但应该可以;我猜主 WordPress Controller 使用 $_SERVER['REQUEST_URI'] 来确定操作。


注意:但是,这不会阻止人们通过转到 URL www.site.com/123 直接访问 123.html。 html - 我一直陷入无限循环,Apache 500 错误试图阻止它:|

关于php - 如何使用 .htaccess 将静态 URL 重定向到动态 URL(在 Wordpress 站点上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com