【问题标题】:Wordpress permalinks: only using the post_id from the URLWordpress 永久链接:仅使用 URL 中的 post_id
【发布时间】:2010-10-07 21:57:00
【问题描述】:

我正在尝试为我的 wordpress 博客设置 SEO 友好的 URL,同时仍然可以灵活地随意更改帖子的标题。

我的永久链接结构是这样的:

%post_id%/%postname%

但是,我希望 wordpress 在寻找合适的帖子时只考虑 URL 中的 %post_id%(有点像 stackoverflow 上的这里)

例如:

https://stackoverflow.com/users/810/crossbrowserhttps://stackoverflow.com/users/810/hello-world

相同

我希望所有这些都指向同一个帖子,即 id 为 345 的帖子:

http://myblog.com/345/the-name-of-the-post
http://myblog.com/345/any-text
http://myblog.com/345

文档中提到了我正在尝试做的事情:Long permalinks,但我无法让它工作。

这是我的 .htaccess 文件:

RewriteEngine on
RewriteBase /

# Let wordpress use pretty permalinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# From the example in the documentation
#RewriteRule /post/([0-9]+)?/?([0-9]+)?/?$ /index.php?p=$1&page=$2 [QSA]

更新

我一直在这个online regular expression testing tool 中尝试这个 RewriteRule,但是当我把它放在我的 .htaccess 中时它不起作用(就在 RewriteBase 之后):

RewriteRule ^([0-9]+)/?([a-zA-Z0-9\-]+)/?$ index.php?p=$1 [QSA]

【问题讨论】:

    标签: regex wordpress .htaccess seo permalinks


    【解决方案1】:

    您可以尝试以下规则:

    RewriteRule ^(\d+)(/[^/]*)?$ index.php?id=$1 [L,QSA]
    

    但我不知道替换是否正确。您必须自己检查。

    【讨论】:

      【解决方案2】:

      我的回答并没有直接回答你的问题,但我觉得我有几点值得注意。

      我正在尝试使用 SEO 友好的 URL 对于我的 wordpress 博客,虽然仍然 可以灵活地改变 帖子标题随意。

      我没有立即看到你追求的好处。

      包含 %post_id% 的明显缺点是它会使 URL 更长并且包含非语义信息。 Wordpress 已经以两种方式处理该场景:

      1. 当您更改已发布帖子的标题时,网址(永久链接/帖子名称)不会改变。

      2. 如果您确实更改了永久链接,旧的永久链接将继续工作并重定向到新的永久链接。

      【讨论】:

      • 我想更改标题,因为我有时会重新考虑我的标题,在这种情况下,我只需要更改永久链接(我的小强迫症方面)。如果#2是真的,那将是完美的。
      【解决方案3】:

      通过使用这个tool,我能够构建一个匹配列表中所有内容的表达式:

      http://myblog.com/435
      http://myblog.com/435/the-post-title
      http://myblog.com/435/the-po
      http://myblog.com/435/asdf
      http://myblog.com/435/the-9887-title
      http://myblog.com/435/123
      

      表达式为:

      http://myblog.com/([0-9]+)/?([a-zA-Z0-9\-]+)/?
      

      这将转化为这条规则:

      RewriteRule ^([0-9]+)/?([a-zA-Z0-9\-]+)/?$ index.php?p=$1 [QSA]
      

      但是,它不起作用。

      【讨论】:

        【解决方案4】:

        我几乎可以通过使用它来工作:

        RewriteEngine on
        RewriteBase /
        
        RewriteRule ^([0-9]+)?/?(^/+)?/?$ /index.php?p=$1 [QSA]
        
        # Allow pretty permalinks
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        

        但是,它并不总是有效,我不确定我理解为什么(或为什么它应该有效)

        它适用于这些:

        http://myblog.com/435
        http://myblog.com/435/the-post-title
        http://myblog.com/435/the-po
        

        但不是这些:

        http://myblog.com/435/asdf (when there's no dash)
        http://myblog.com/435/the-9887-title (whenever there's a number in the post title)
        

        有什么帮助吗?

        【讨论】:

        • 您应该使用规范的 URI。
        • 只有你的 PHP 应用程序才能做到这一点,因为只有在给出 ID 时它才能获得条目标题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-18
        • 2012-05-07
        相关资源
        最近更新 更多