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]
You could try the following rule:
But I don’t know if the substituion is correct. You have to check that by yourself.
My response does not directly answer your question, but I think I have some points worth noting.
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:
The url (permalink/post name) doesn’t change when you change the title on a published post.
If you do change the permalink, the old permalink continues to work and redirects to the new permalink.
By using this tool, I was able to build an expression that matches everything in my list:
The expression is:
which would translate to this rule:
however, it doesn’t work.
I almost got it working by using this:
however, it doesn’t always work and I’m not sure I understand why (or why it should work)
It works with these:
but not with any of these:
Any help?