Adding custom WP permalink using .htaccess

How to add custom WP permalink using .htaccess and give your blog an SEO boost?

By using RewriteRule on .htaccess we can modified an ordinary url to a pretty url. Examples of such ordinary url is

pretty url is

As we can see, pretty url have cleaner format and more meaningful rather than ordinary url, a visitor or probably search engine can have more understanding of what the url try to represent.

The rule for the pretty link is this:

RewriteRule ^/buy/(.+)/(.+)/?$ / buy.php?product=$1&color=$2

Don’t forget to set RewriteEngine On before doing RewriteRule, the full code like this.

RewriteEngine On
RewriteBase /
RewriteRule ^/buy/(.+)/(.+)/?$ /buy.php?product=$1&color=$2

Now, how to set this RewriteRule in WordPress? Please note, WordPress will rewrite the .htaccess file everytime you load/open Settings->Permalink page, the rewritten is all that in between

# BEGIN WordPress
# END WordPress

This process will truncate our custom rule which we add manually between WordPressRule.

So how to avoid wp from truncating our .htaccess Rewrite rules? We can use the wp codex Rewrite API: add_rewrite_rule in our theme functions.php, for example:

if (function_exists ('rc_pretty_url')):

function rc_pretty_url () {

/ / Load this function before everything else
add_action ('init', 'rc_pretty_url');


This function will add a RewriteRule in .htaccess file (without saving it to the database) at the the time Settings->Permalink page opened/loaded. So don’t forget to load the Settings->Permalink page after adding/changing this function or it won’t work.

That’s it, now your WordPress will have a new custom permalink.

In the next article I will review another way to customize the permalink without changing the .htaccess file.

Be Sociable, Share!

1 Response(s) to "Adding custom WP permalink using .htaccess"

  • Daniel


    OCT 02 Reply

    URLRewrite is supported out of box on linux based hosting servers, for windows hosting servers thirdparty url rewrite utilities are needed.

Leave a Comment

Math Captcha: Prove that you are human by answering this question *