WordPress的永久链接:只使用网址中的post_id

问题描述:

我想为我的WordPress博客提供SEO友好的URL,同时仍然可以随意更改帖子的标题。看时WordPress的永久链接:只使用网址中的post_id

%POST_ID%/%postname%

不过,我想WordPress的只是考虑%POST_ID%来自网址:

我的永久链接结构会是这样为适当的职位(有点像这里计算器)

例如:

https://stackoverflow.com/users/810/crossbrowser相同https://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] 

UPDATE

我一直在这个online regular expression testing tool尝试这种重写规则,但是当我把它放在我的.htaccess不工作(刚好在RewriteBase之后):

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

我的回答并不直接回答你的问题,但我认为我有一些值得注意的观点。

我想为我的WordPress博客搜索引擎友好的URL ,同时还 具有灵活性随意更改 文章的标题。

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

包含%post_id%的明显缺点是它使URL更长,并且包含非语义信息。 WordPress的已经与该方案涉及在两个方面:

  1. 的URL(永久/后的名称),当您更改在发布的帖子标题不会改变。

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

你可以尝试以下规则:

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

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

我几乎得到它的工作通过使用这样的:

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) 

任何帮助?

+0

您应该使用规范的URI。 – Gumbo 2009-02-16 17:56:31

+0

请注意详细说明吗? – mbillard 2009-02-16 18:45:19

通过使用此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] 

但是,它不起作用。