I have a custom URL like this:


and I'm trying to come up with a mod_rewrite rule to convert to


/show/ is a page name.

Here's the rules I'm using in .htaccess:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This is working, it rewrites




Then I modified the rule for the second query string:

RewriteRule ^show/(.*)/(.*)$ /show/?id=$1&n=$2 [R=301,NC,QSA]

But website.com/show/9999/page-name/ returns a 404 error.
It works if I go to: website.com/show/9999/?n=page-name.

What am I doing wrong?

  • First if all, you don't have to change the rule in the htaccess by yourself. This is a general "problem" of WordPress and mostly can be solved by quickedit the posts, change nothing and save.
    – marvinpoo
    Jan 4 '17 at 9:31
  • I don't think you understand, these are non-WordPress pages.
    – Bryan
    Jan 4 '17 at 20:20
  • please post your solution as a proper answer ;) as for the last part left, it probably best done in an htaccess redirect rule (best here means faster). The other option is to detect those $-GET parameters and redirect at the PHP side Jan 5 '17 at 4:35
  • I think the above issues can be done by changing permalinks of wordpress. Dec 27 '18 at 7:07

I think I figured it out... almost. I needed to use WordPress' own rewrite engine to define the rewrite rules so it recognizes what I'm doing.

So I added this to my WordPress theme's functions.php:

add_action( 'init', 'init_custom_rewrite' );

function init_custom_rewrite() {
        'top' );

add_filter('query_vars', 'my_query_vars', 10, 1);

function my_query_vars($vars) {
    $vars[] = 'id';
    $vars[] = 'n';
    return $vars;

The URL website.com/show/9999/page-name works correctly.

Now I need to redirect the old query string URL: website.com/show/?id=9999&n=page-name to the new SEO friendly url: website.com/show/9999/page-name

I do that with some rewrite rules in .htaccess:

RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$ 
RewriteRule ^showtest/?$ /showtest\/%1\/%2\/? [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.