Wordpress permalinks: only using the post_id from

2019-06-04 21:52发布

I'm trying to have SEO friendly URLs for my wordpress blog, while still having the flexibility to change a post's title at will.

My permalink structure would be like this:

%post_id%/%postname%

However, I'd like wordpress to just consider the %post_id% from the URL when looking for the appropriate post (sort of like here on stackoverflow)

For example:

https://stackoverflow.com/users/810/crossbrowser is the same as https://stackoverflow.com/users/810/hello-world

I'd like all of these to point to the same post, the one with id 345:

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

The documentation mentions something that seems like what I'm trying to do: Long permalinks, but I couldn't get it to work.

Here's my .htaccess file:

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

I keep trying this RewriteRule in this online regular expression testing tool, but it doesn't work when I put it in my .htaccess (just after RewriteBase):

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

4条回答
爷、活的狠高调
2楼-- · 2019-06-04 22:42

You could try the following rule:

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

But I don’t know if the substituion is correct. You have to check that by yourself.

查看更多
Lonely孤独者°
3楼-- · 2019-06-04 22:44

I almost got it working by using this:

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]

however, it doesn't always work and I'm not sure I understand why (or why it should work)

It works with these:

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

but not with any of these:

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)

Any help?

查看更多
叼着烟拽天下
4楼-- · 2019-06-04 22:51

By using this tool, I was able to build an expression that matches everything in my list:

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

The expression is:

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

which would translate to this rule:

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

however, it doesn't work.

查看更多
再贱就再见
5楼-- · 2019-06-04 22:55

My response does not directly answer your question, but I think I have some points worth noting.

I'm trying to have SEO friendly URLs for my wordpress blog, while still having the flexibility to change a post's title at will.

I'm not immediately seeing the benefit of your pursuit.

The obvious disadvantage of including the %post_id% is that it makes the URL longer and contains non-semantic information. Wordpress already deals with the scenario in two ways:

  1. The url (permalink/post name) doesn't change when you change the title on a published post.

  2. If you do change the permalink, the old permalink continues to work and redirects to the new permalink.

查看更多
登录 后发表回答