I was recently asked to add an extra path component to the URL of a WordPress page for SEO reasons. It took some diving into the internals of WordPress. Here’s the setup:
- WordPress is installed in the site root, at example.com
- We are using a static homepage
- The company blog is at example.com/blog/
- I was asked to make the blog URL be example.com/blog/some-extra-keywords/ without changing the URL of the current and future blog posts, which are currently example.com/blog/post-name/
There are 3 things required to make this work:
- A filter to tell WordPress to process requests for /blog/some-extra-keywords/ as if they are for /blog/
- A filter to change permalinks from /blog/ to /blog/some-extra-keywords/ (Optional, but recommended).
- A redirect from /blog/ to /blog/some-extra-keywords/ (Also optional, but also recommended).
Treat /blog/some-extra-keywords/ requests like /blog/
We don’t want to change the URL of the blog page in WordPress’s Edit Page screen because that will change the URL of past and future all blog posts. We need to tell WordPress that a request for /blog/some-extra-keywords/ is a request for /blog/. This is done by adding a filter to WordPress’s request
filter hook:
/**
* Make the URL /blog/some-extra-keywords/ respond as /blog/
* would.
* @param array $request The request array that WP generates.
* @return array The modified request array.
*/
function jb_filter_blog_url_request( $request ) {
if ( isset( $request['name'] ) && 'some-extra-keywords' == $request['name'] ) {
// it's a page we're requesting. If you're doing this to something other
// than a page, try setting $request['name'], and don't unset it 2 lines
// later
$request['pagename'] = 'blog';
unset( $request['name'] );
}
return $request;
}
add_filter( 'request', 'jb_filter_blog_url_request' );
Since WordPress identifies pieces of content by the last part of the URL, (the “name”), that’s all we have to change. In this case, because the blog is on a page, we had to set $request['pagename']
.
Change links from /blog/ to /blog/some-extra-keywords/
Note: This is optional, but recommended. If you don’t do this you must do the next item, (redirect /blog/ to /blog/some-extra-keywords/), but you get the most SEO benefit if you do both.
With #1 it’s time to change links to the blog page to point to the new URL. The old URL still works, and you’ll be redirecting it in Step 3, but modifying the links cuts out a round-trip to the server, making your site faster, and it means that anytime a spider crawls the site it will find the correct URL.
Again, we create a filter function and hook it up. Because I’m doing this all to a page, I’m hooking on to the page_link
filter, but the function also works with the post_link
filter. In fact, I originally used the post_link
filter, which doesn’t work for pages, and spent a while banging my head against a wall trying to figure out why it didn’t work.
By hooking into page_link
or post_link
you’ll modify the URL everywhere it is generated with get_permalink()
, which is almost everywhere. Menus, lists of pages, and even the XML Sitemap that Yoast’s WordPress SEO plugin makes will be have the new URL. If you have typed the URL somewhere this filter will not change it, but that’s why you’ll set up a redirect in Step 3.
The code:
/**
* Modifies the blog URL when it's requested using get_permalink().
*
* Note that this filter is set up to work on both posts & pages, and the
* post_link and page_link filters don't pass quite the same parameters:
* - $post is an post object for post_link and a post ID for page_link, so we
* get the post object if $post is numeric
* - $dontuse is completely different between the two filters, but not needed,
* so we ignore it.
*
* @param string $url The URL to be filtered.
* @param mixed $post The post ID or post object that corresponds to $url
* @param mixed $leavename Differs depending on filter. Ignore.
* @return string The possibly-modified URL.
*/
function jb_modify_blog_url( $url, $post, $leavename ) {
$true = false;
// don't do it in the admin, I'm afraid the modified URL will get
// added to the URL slug field on the Edit Page screen, and get
// permanently added, with another copy of it being added every time
// the page is saved.
if( ! is_admin() ) {
if ( is_numeric( $post ) ) {
$post = get_post( $post );
}
if( 'blog' == $post->post_name ) {
$url .= 'some-extra-keywords/';
}
}
return $url;
}
add_filter( 'page_link', 'jb_modify_blog_url', 10, 3 );
The magic is in the third if condition: If the page name is ‘blog’ add the extra keywords to the URL.
Redirect from /blog/ to /blog/some-extra-keywords/
Note: This is optional, as well, but if you don’t do it the blog homepage will be available at both /blog and /blog/some-extra-keywords/ which could lead to a duplicate content penalty from Google.
We’re going to do a 301 Redirect to tell search engines that the blog is now at /blog/some-extra-keywords/. I’m going to do this in a .htaccess file because that way the web server doesn’t have to start PHP or WordPress.
If you’ve already got a rewrite section in your .htaccess file, add the following line to it:
RewriteRule ^blog/?$ /blog/some-extra-keywords/ [R=301,L]
This will redirect both /blog/ and /blog, (the first ? makes the slash right before it optional), to /blog/some-extra-keywords/. If you don’t have a rewrite section, add one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog/?$ /blog/whole-house-fan-energy-saving-tips/ [R=301,L]
</IfModule>
And that’s it.