Support » Developing with WordPress » Adding custom rewrite rule to my blog

  • Hi

    I am wanting to add a custom rewrite rule that forces any URL like:

    To rewrite (not redirect) to a specific wordpress post (post ID 255) and pass the “card-one”, “card-two” as a query string, so it can be accessed within the wordpress post (using Exec-PHP plugin).

    How can I add such a custom rewrite rule, and how would I write such a rule?

    btw these /card/ urls do not conflict with any of my blog’s normal url structure.


Viewing 6 replies - 1 through 6 (of 6 total)
  • rewrites are tough, especially with wordpress, I’ll try to answer as best I can.
    this is an example I’ve used to rewrite articles instead of blog posts

    RewriteRule ^articles/(.*)/(.*)/$ /articles/index.php?cat=$1&art=$2 [L]

    That rewrite will pass real-estate as the cat and how-to-find-a-home as the art (article title). I pass this to a wordpress custom template page, which has php code to look up the article in our article folder. the article folder is named real-estate. I placed this rewrite before all other rewrites so if it is a match it will process it, else, wordpress permalinks will probably throw out a 404.

    hope that helps

    Duh, actually I don’t pass it to a custom template page, I pass it to index.php in the article folder.
    The first line of index.php will have an include wp-config.php so you can access all the wordpress functions liek get_header()


    here is a blog post I wrote about accessing wordpress functions outside your theme folder

    Thanks, I’ve tried add the following:
    RewriteRule ^card/(.*)/$ /index.php?page_id=215&card=$1 [L]
    To my htaccess and it breaks the site.
    Htaccess below:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^card/(.*)/$ /index.php?page_id=215&card=$1 [L]
    RewriteRule . /index.php [L]

    I just want to rewrite etc, to my specific WordPress page (#215).

    Hope that makes sense.

    if you want to make a specific page, you should make a page template file in your wordpress theme. then make a page called card, and use the page template you made. in the file add

    $card_id = $_GET['card'];
    $post = get_post($_GET['page_id'])
    //do stuff with post info

    then in the .htaccess, rewrite to that page template. You will be able to access $_GET[‘card’] in the page template file.

    RewriteRule ^card/(.*)/$ /card/?page_id=215&card=$1 [L]

    I think index.php just sets up wordpress and passing a post id to it will do nothing.




    have you been able to solve it? Could you, please, post the solution?

    I am new to wordpress, so I may be wrong, but it seems like it could help me with my problem – my wordpress page has a link to the html file /html/externalfiles/index.html which itself contains links to other files in the /html/externalfiles/ directory. I need to make sure that only authenticated users can access /html/externalfiles/(html files.


    Small note regarding your rewrite rules, you ideally should use (.+) for pattern matching as (.*) is 0 or more characters(and it’s slower), where as (.+) is any character once or more and i imagine you’re expecting at least one character there.

    There’s also not necessarily any need to be writing your own rules in the htaccess file directly (i tend to do that myself though), but for reference see the link below for some example rules.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding custom rewrite rule to my blog’ is closed to new replies.