【问题标题】:Wordpress Permalinks: Remove tag base and add the .html extensionWordpress 永久链接:删除标签库并添加 .html 扩展名
【发布时间】:2011-07-06 03:47:43
【问题描述】:

我的问题:

My permalink settings:

常规设置> 自定义 > /%postname%.html

可选设置>标签库>标签


我想要这个:mysite.com/%tag-slug%.html

谢谢


为@Dimitry 编辑

正是我想要的方式

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        // problem. returning: http://www.domain.com/post-tag/tag-name
        //$tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link);

        // I added it. Result: http://www.domain.com/tag-name
        $tag_link = str_replace("$tag_base/", "", preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link)); 
        //echo "$tag_link<br>";
    }
    // problem. returning: http://www.domain.com/http://www.domain.com/tag-name.html
    //return '/' . trim($tag_link, '/') . '.html';

    // I added it. Result: http://www.domain.com/tag-name.html , 
    return trim($tag_link, '/') . '.html';

}
?>

我做了永久链接设置

常规设置> 自定义 > /post-%postname%.html 可选设置>标签库>后标签

.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

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

单页:http://www.domain.com/post-hello-world.html >> 正在工作 标签页:http://www.domain.com/tag-name.html >> 未找到 404 http://www.domain.com/post-tag/tag-name >> 工作中

问题:

找不到标签页

对不起。我是初学者。谢谢迪米特里

【问题讨论】:

  • 我猜一个 wordpress 论坛会带来更好的结果......这个问题可能有点过于本地化,而不是严格意义上的编程问题。
  • 所以您希望您的帖子被他们的标签 + .html 引用?还是您希望标签以“.html”结尾?
  • 我想要这个东西;标签页面网址:mysite.com/tag-slug.html 发布页面网址:mysite.com/post-slug.html 谢谢

标签: php wordpress mod-rewrite permalinks


【解决方案1】:

如果我错了,请纠正我,但我认为由于内容重复而无法做到。

【讨论】:

    【解决方案2】:

    我不确定您可以通过管理界面执行什么操作,但您可以添加一些自己的代码作为插件(在 wp-content/plugins/tag_links.php 中):

    在您的 .htaccess 文件中,在其他重写规则之上,执行以下操作:

    # 将 /tag-name.html 重写为 /post-tag/tag-name # 假设您的 tag_base 选项为空或设置为 'post-tag' RewriteCond %{REQUEST_URI} !^/post-.* RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

    您的帖子 URL 结构将是 post-%postname%.html。

    所有这一切都表明您的标签和帖子必须在 URL 结构中至少具有一项差异化功能。在这种情况下,它是帖子 URL 的“post-”前缀。

    您正在构建 Wiki 吗?

    【讨论】:

    • 感谢您的长篇回复。一切正常。但是有一些问题。我已经编辑了我的问题。请检查。谢谢。
    • 嘿约翰。我给的 htaccess 规则必须在 RewriteBase 和第一个 RewriteRule 之间。
    • @Dimitry 我做了。但我不知道,对吧?标记页面仍然无法正常工作(错误 404)。 new htaccess
    • @John,似乎解决方案需要的不仅仅是简单的 mod_rewrites。我会考虑向重写模块添加规则:codex.wordpress.org/Function_Reference/WP_Rewrite
    • @Dimitry 我做了。它的运行。谢谢你的一切。 add_filter('tag_rewrite_rules', 'my_func');
    【解决方案3】:

    嗨试试这个http://wordpress.org/plugins/import-html-pages/

    将格式良好的静态 HTML 文件导入 WordPress。需要 PHP 5。

    此插件将导入文件目录作为页面或帖子。您可以指定包含要导入的内容的 HTML 标记(例如 、 或 )或 Dreamweaver 模板区域(例如“主要内容”)。

    如果导入页面,目录层次结构将被保留。包含指定文件类型的目录将作为空父页面导入(或者,如果存在索引文件,则其内容将用于父页面)。不包含指定文件类型的目录将被忽略。

    导入文件时,将显示生成的 ID、永久链接和标题。完成后,导入器将提供 Apache 重定向列表,可在您的 .htaccess 文件中使用,以将访问者从旧文件位置无缝转移到新的 WordPress 永久链接。从 2.0 开始,如果您在导入文件后更改永久链接结构,则可以重新生成重定向 - 文件的旧 URL 作为自定义字段存储在导入的帖子中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多