更改永久链接 - 需要 htaccess 代码

发布于 2025-01-07 05:52:13 字数 656 浏览 0 评论 0原文

我有一个博客(wordpress 版本 3.01),它具有以下格式的基于自定义的永久链接:/%category%/%postname%/。例如,blog.com/category/post-name/

我需要将永久链接更改为“日期和名称”/%year%/%monthnum%/%day%/%postname%/(我的主题要求它才能正常工作)。

我需要在 .htaccess 文件中写入什么以使旧的永久链接 301 重定向到新的永久链接?

我的网站已有 9 个月的历史,约有 500 篇文章。


示例

/%category%/%postname%/ 到 /%postname%/

RedirectMatch 301 ^/([^/]+)/([^/]+)/$ http://www.mydomain.com/$2

我的情况是将此

/%category%/%postname%/ 发送到/%year%/%monthnum%/%day%/%postname%/

RedirectMatch 301 ????????????

I have a blog (wordpress version 3.01) which has custom based permalinks in the following format: /%category%/%postname%/. For example, blog.com/category/post-name/.

I need to change permalinks to "Day and name" /%year%/%monthnum%/%day%/%postname%/ (my Theme requires that to work properly).

What do I need to write in my .htaccess file to make the old permalinks 301 to redirect to the new ones?

My site is 9 months old and has about 500 articles.


Example

/%category%/%postname%/ to /%postname%/

RedirectMatch 301 ^/([^/]+)/([^/]+)/$ http://www.mydomain.com/$2

My case is to get this

/%category%/%postname%/ to /%year%/%monthnum%/%day%/%postname%/

RedirectMatch 301 ???????????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2025-01-14 05:52:13

您无法使用 .htaccess 文件执行此操作,因为可以执行此操作的重写功能 (RewriteMap) 在 .htaccess 上下文中不起作用。但是,由于您有一个封闭列表,因此有一个固定的类别列表,因此将它们映射到通用重定向器脚本很简单:(

RewriteEngine On
RewriteBase   /

RewriteRule ^(cat1|cat2|cat3|cat4)/.*?/$ redirector.php?cat=$1&post=$2 [L,QSA]

将猫列表扩展为完整的类别列表)。那么 redirector.php 是一个标准的重定向器模式。您只需在退出之前查询您的 WordPress D/B 即可确定要发布的帖子的 YMD

$server  = $_SERVER['HTTP_HOST'];
header( "Location: HTTP://$server/$year/$month/$day/$postName/", true, 302 );

You cant do this with an .htaccess file as the rewrite feature (RewriteMap) which could do this doesn't work in a .htaccess context. However, since you've got a closed list and therefore a fixed list of categories, it is simple to map these to a common redirector script:

RewriteEngine On
RewriteBase   /

RewriteRule ^(cat1|cat2|cat3|cat4)/.*?/$ redirector.php?cat=$1&post=$2 [L,QSA]

(extending the cat list to be a complete list of categories). Then redirector.php is a standard redirector pattern. You simply query your Wordpress D/B to determine the YMD for the post to issue a

$server  = $_SERVER['HTTP_HOST'];
header( "Location: HTTP://$server/$year/$month/$day/$postName/", true, 302 );

before exiting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文