【问题标题】:Adding .html onto the end of a URL将 .html 添加到 URL 的末尾
【发布时间】:2011-01-05 11:42:37
【问题描述】:

我目前使用 Wordpress 作为博客平台,但我想改用 Jekyll 来生成静态页面。在 Wordpress 上,我的 URL 使用以下格式:

/年/月/日/标题

但我想将其重定向到

/年/月/日/title.html

我不知道如何使用 mod_rewrite 来做到这一点。

有人有什么想法吗?

【问题讨论】:

    标签: wordpress url mod-rewrite jekyll


    【解决方案1】:

    我相信您可以转到管理 → 设置 → 永久链接并将永久链接设置为自定义,值为:

    /%year%/%monthnum%/%day%/%postname%.html

    【讨论】:

    • 不再使用 Wordpress(参见 OP),我不想失去任何谷歌排名。
    • 啊,我以为您的意思是您想使用 Wordpress 发布帖子,但将 url 与 Jekyll(用于页面)保持一致。
    • 我在想这个,但是如果有人试图访问任何当前页面,他们会得到 404。我正在努力避免这种情况。
    • 你可能想测试一下——WP 做了一些额外的事情。例如,如果你有一个完整的永久链接结构,但尝试转到 /postname,WP 会重定向你。
    • 现在一切都很好,但我肯定会走 jekyll 路线,所以我会想使用 mod_rewrite
    【解决方案2】:
    RewriteEngine On
    # Only if the URI is not a normal file
    RewriteCond %{REQUEST_FILENAME} !-s 
    # ... or a symbolic link
    RewriteCond %{REQUEST_FILENAME} !-l 
    # ... rewrite everything that ends on .html to the stripped down URL
    RewriteRule (.+)\.html$ $1 [L]
    # Alternatively, if you want to be more specific about the scheme, you can use this
    # RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)\.html$ $1/$2/$3/$4 [L}
    

    以上内容应为您提供一些关于如何将 URL 正确重写为所需方案的指示。此示例透明地将所有以 .html 结尾的内容(实际文件除外)重写为相同的 URL,而没有附加 .html。

    【讨论】:

    • 如果我想让它做一个实际的重定向(也可能是 301),我会做 "RewriteRule (.+)\.html$ $1 [L,R,301]" (不带引号, obv)?
    • 对不起,我误读了您在重写规则中的内容。它应该采用当前 URL 并将 .html 放在末尾。类似于以下内容: RewriteRule (.+)$ $1.html [R=301,L]
    猜你喜欢
    • 2013-08-07
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2012-08-03
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    相关资源
    最近更新 更多