views:

728

answers:

4

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:

http://stackoverflow.com/users/810/crossbrowser is the same as http://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]
+2  A: 

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.

Gumbo
A: 

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?

GoodEnough
You should use canonical URIs.
Gumbo
Care to elaborate on that?
GoodEnough
Only your PHP app could do that as only it can obtain the entry title when just the ID is given.
Gumbo
+1  A: 

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.

GoodEnough
+2  A: 

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.

Lloyd Budd
I want to change title because I tend to rethink my titles sometimes and in those cases, I just need to change the permalink (my little OCD side).If #2 is true though, that would be perfect.
GoodEnough
I tested #2 and it does work, thanks a lot.
GoodEnough