WordPress.org

Ready to get started?Download WordPress

Forums

Adding custom rewrite rule to my blog (7 posts)

  1. idealists
    Member
    Posted 4 years ago #

    Hi

    I am wanting to add a custom rewrite rule that forces any URL like:
    mysite.com/card/card-one/
    mysite.com/card/card-two/
    mysite.com/card/another-card/

    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.

    Thanks

  2. Matt Dunlap
    Member
    Posted 4 years ago #

    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
    /articles/real-estate/how-to-find-a-home/

    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

  3. Matt Dunlap
    Member
    Posted 4 years ago #

    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()

    include('../wp-config.php')

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

  4. idealists
    Member
    Posted 4 years ago #

    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]
    </IfModule>

    I just want to rewrite mysite.com/card/card-one/ etc, to my specific WordPress page (#215).

    Hope that makes sense.

  5. Matt Dunlap
    Member
    Posted 4 years ago #

    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'])
    setup_postdata($post);
    //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.

  6. irina195
    Member
    Posted 4 years ago #

    Hello,

    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.

    Thanks!

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

    http://codex.wordpress.org/Function_Reference/WP_Rewrite

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.