WordPress Code Snippets

A fresh mix of code snippets and tutorials

Remove the date from WordPress permalinks

27

With the rise of the “short URL”, including the date in URLs is no longer considered “cool”, so you may want to “get with it” and dumb down your WordPress URLs to make them shorter. Here is how to use .htaccess to remove the date from your WordPress permalinks.

To get invited to the cool parties, just add the following snippet to the .htaccess file located in your root directory:

Step 1: Update your WP Permalink Settings

Log in to the WP Admin and change your permalink structure to whatever you would like; i.e., something without a date in it. For example, “Post name only” is now a popular choice:

/%postname%/

Step 2: Redirect old URLs with mod_rewrite

Next, add either of the following to your site’s root .htaccess file:

If your OLD permalinks look like this: /%year%/%monthnum%/%day%/%postname%/

Then add this .htaccess code: RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ /$4 [R=301,NC,L]

If your OLD permalinks look like this: /%year%/%monthnum%/%postname%/

Then add this .htaccess code: RewriteRule ^([0-9]+)/([0-9]+)/(.*)$ /$3 [R=301,NC,L]

If your OLD permalinks look like this: /%year%/%postname%/

Then add this .htaccess code: RewriteRule ^([0-9]+)/(.*)$ /$3 [R=301,NC,L]

If you would like to consolidate your code, the directive from this step may be combined with WordPress permalink rules (also located in .htaccess file). Here is an example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/(.*)$ /$3 [R=301,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

That’s really all there is to it. It should be mentioned that this method uses Apache’s mod_rewrite for the redirects, but it’s not the only way to do it..

Alternate Step 2: Redirect old URLs with mod_alias

Instead of using Apache’s rewrite module to redirect old links, we can use mod_alias instead. Apply whichever of these possible directives to your root .htaccess file:

If your OLD permalinks look like this: /%year%/%monthnum%/%day%/%postname%/

Then use this: RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) http://example.com/$1

If your OLD permalinks look like this: /%year%/%monthnum%/%postname%/

Then use this: RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([a-z0-9\-/]+) http://example.com/$1

If your OLD permalinks look like this: /%year%/%postname%/

Then use this: RedirectMatch 301 ^/[0-9]{4}/([a-z0-9\-/]+) http://example.com/$1

Results

Once steps 1 and 2 are complete, your permalinks will go from, say, this (or whatever):

http://example.com/2012/12/12/example-post/

..to this:

http://example.com/example-post/

Much better! Now grab some juice and hit the party :)

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!