WordPress.org

Ready to get started?Download WordPress

Forums

How to perform URL redirection using mod_rewrite in 2.0 beta (13 posts)

  1. tinyau
    Member
    Posted 8 years ago #

    I have raised these questions in another thread Permalinks and Edit posts WP 2.0 beta. I think someone may overlook it, so I start a new thread to ask it agin.

    In version 1.5.2, I use mod_rewrite to redirect all my post feed and comment feed to Feedburner, and use the mod_rewrite of Ultimate Tag Warrior (UTW) plugin to use a simpler URL /tag/tag instead of /index.php?tag=tag.

    I have tried to use .htaccess file of version 1.5.2 in version 2.0 Beta 1 without any problem. I found that whatever permalink structure I used, the content of .htaccess file still contained the following few lines.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php
    </IfModule>

    I love the simplicity of the content of .htaccess file in 2.0. I tried to use it but I have some problems if someone can lead me a hand to solve it.

    1. How to disable the mod_rewrite of post feed and comment feed so that I can add the entries of redirection of feed to Feedburner?
    2. I have added the following entries in .htaccess that generated by UTW of /tag/tag URL redirection to the .htaccess of version 2.0 Beta 1. However, the redirection failed. How can I solve it?

    RewriteRule ^tag/?(.*)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?tag=$1&feed=$2 [QSA,L]
    RewriteRule ^tag/?(.*)/page/?(.*)/$ /index.php?tag=$1&paged=$2 [QSA,L]
    RewriteRule ^tag/?(.*)/$ /index.php?tag=$1 [QSA,L]
    RewriteRule ^tag/?(.*)/page/?(.*)$ /index.php?tag=$1&paged=$2 [QSA,L]
    RewriteRule ^tag/?(.*)$ /index.php?tag=$1 [QSA,L]

  2. Ryan Boren
    WordPress Dev
    Posted 8 years ago #

    Preferably, plugins shouldn't write directly to .htaccess. Instead, they should write to WP's internal rewrite structure and let WP handle it.

    The Feed director plugin provides a simple example of how to do this.

  3. This is my .htaccess file. It works like a charm. :)

    http://pastebin.com/436659

    Ex: http://www.viper007bond.com/feed/rss2/

  4. tinyau
    Member
    Posted 8 years ago #

    Thanks Viper007Bond. After I moved all my rewrite rules to the beginning of .htaccess file, and let those entries of WordPress in the end. It works like a charm. ;)

  5. blueorder
    Member
    Posted 8 years ago #

    Viper,

    The link to your .htaccess is broken (as of this post). Could you repost it? Thanks

  6. jaseone
    Member
    Posted 8 years ago #

    Ryan,

    How would you use WP's internal rewrite structure to rewrite:

    tags/tagname

    into:

    index?page_id=271&tag=tagname

    Where tagname could be anything.

    I played a bit with it trying to follow your example but haven't been able to get quite there, can you do it with internal wordpress functions or should I just parse the requested URL to grab it?

  7. jaseone
    Member
    Posted 8 years ago #

    What I currently have:

    function taggerati_rewrite($wp_rewrite) {
    $taggerati_rules = array(
    'tags/(.+)' => '/index.php?page_id=271&tag='.ltrim($_SERVER['REQUEST_URI'],"/tags/")
    );
    $wp_rewrite->rules = $taggerati_rules + $wp_rewrite->rules;
    }
    // Hook in.
    add_filter('generate_rewrite_rules', 'taggerati_rewrite');

    That gets me as far as rewriting /tags/anything to ?page_id=271 but the second GET parameter never seems to be applied even if I hardcode it to one of my tags so it doesn't load the specific tag page just the generic tags page, what am I doing wrong?

  8. Viper,

    The link to your .htaccess is broken (as of this post). Could you repost it? Thanks

    It's working as of the time of this post.

  9. jaseone
    Member
    Posted 8 years ago #

    Okay I officially give up...

    I have tried using WP's internal rewrite rules and .htaccess but in no way can I get this to work:

    /tags/tagname --> /index.php?tag=tagname

    Trying to do it in .htaccess with:

    RewriteRule ^tags/?(.*)$ /index.php?tag=$1 [NC,L]

    Just results in a 404 and using WP's internal rewrite rules I can't for the life of me set a GET variable.

    So how can you do this?

  10. jaseone
    Member
    Posted 8 years ago #

    Okay on a whim I tried the other method in the Codex:

    function keywords_createRewriteRules($rewrite) {
    global $wp_rewrite;

    // add rewrite tokens
    $keytag_token = '%tags%';
    $wp_rewrite->add_rewrite_tag($keytag_token, '(.+)', 'tag=');

    $keywords_structure = $wp_rewrite->root . "tags/$keytag_token";
    $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);

    return ( $rewrite + $keywords_rewrite );
    }
    add_filter('search_rewrite_rules', 'keywords_createRewriteRules');

    That works as long as I ALSO have the below in my .htaccess:

    RewriteRule ^tags/?(.*)$ /index.php?tag=$1 [NC,L]

    One will not work without the other, what is up with that?

  11. ChristineD
    Member
    Posted 8 years ago #

    If someone could give me a hand with wrangling whatever I need to wrangle to get UTW to do the rewriting stuff; that would be super {:

    I've tinkered with changing the contents of the action to look like both the example here: http://wordpress.org/support/topic/50182#post-277530 and here: http://boren.nu/downloads/feed_director.phps and in both situations, when I go to a theoretically rewritten tag page, I get what looks like the homepage instead.

    I've done a little bit of digging in the code responsible for the url rewriting; and for a tag page, my handy debug info says: Got a match:tag/?(.*)$ query is tag=$1 whereas for a category page, I get: Got a match:category/(.+?)/?$ query is category_name=gah (query is the value of $query after the call to parse_str; before the value of _GET["error"] is checked..)

    Category looks like it should work; and I can see why tags wouldn't d:

    I'm just not sure whether it's a UTW problem or a wordpress problem.

    (Incidentally, if I tried using a tag named "$1", it didn't work either. If I go to ?index.php?tag=sometag it works; so the underlying tag-getting bit seems to be working)

  12. ChristineD
    Member
    Posted 8 years ago #

    I should add; the currently tinkered with version of the rewrite filter is this:

    function ultimate_rewrite_rules($existingrules) {
    if(get_option("utw_use_pretty_urls") == "yes") {
    $baseurl = get_option("utw_base_url");
    $rules = new array();
    $rules[substr($baseurl, 1) . "?(.*)/feed/(feed|rdf|rss|rss2|atom)/?$"] = "index.php?tag=$1&feed=$2";
    $rules[substr($baseurl, 1) . "?(.*)/page/?(.*)/$"] = "index.php?tag=$1&paged=$2";
    $rules[substr($baseurl, 1) . "?(.*)/$"] = "index.php?tag=$1";
    $rules[substr($baseurl, 1) . "?(.*)/page/?(.*)$"] = "index.php?tag=$1&paged=$2";
    $rules[substr($baseurl, 1) . "?(.*)$"] = "index.php?tag=$1";
    }
    return $rules + $existingrules->rules;
    }

    Just so you don't have to go digging around for it (:

  13. ChristineD
    Member
    Posted 8 years ago #

    I figured it out for maself d:

    Making URL rewriting* go in wordpress 2.0 RC1

    * That is, when you need to snag values from the query string

Topic Closed

This topic has been closed to new replies.

About this Topic