• Hello,

    I am trying to figure out how to rewrite this type of URL

    http://site-name/category/floppy-hair-beach/?tdo_tag=lama-jonny-2

    into

    http://site-name/category/floppy-hair-beach/tag/lama-jonny-2

    I have got this in my .htaccess file but it does not rewrite, what am I doing wrong??

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteRule ^tag/(.*)$ ?tdo_tag=$1 [L]
    </IfModule>

    # END WordPress

    Thank you for your help!
    P

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Do either of those URLs work directly? I mean when you type them into your browser.

    Try moving this line

    RewriteRule ^tag/(.*)$ ?tdo_tag=$1 [L]

    To be above this line

    # BEGIN WordPress

    Not sure if that will, could, or should do what you think it will though. Also you may want to remove the [L] from that line.

    Thread Starter Pierrick

    (@artpie)

    Thanks Jan

    I tried what you suggested but it did not work…

    Then I realised I had forgotten (I think) a / before the ?tdo_… so I tried this

    RewriteRule ^tag/(.*)$ /?tdo_tag=$1 [L]
    inside and outside the # BEGIN WordPress with and without the [L] but still no result so I am completely puzzled here!

    Anyone can tell me what I am doing wrong??
    Cheers

    P

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    After I read what I’d written, I realized that what I meant to say was add these lines above your # BEGIN WordPress like so

    # Tag rewrite
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^category/(.*)/\?tdo_tag=(.*) http://site-name/tag/$2 [L]
    </IfModule>
    
    # BEGIN WordPress

    Don’t bother copying those lines, it won’t work. πŸ™‚

    There may be a way to do it with mod_rewrite but it’s really search and replace on the URL that you want. My mod_rewrite is good, but not that good.

    So use a plugin instead. I got the idea and code from this web site.

    In wp-content/plugins create a file called tdo-tag-replace.php and paste in these lines.

    <?php
    /*
    Plugin Name: TDO tag rewrite
    */
    
    add_action( 'template_redirect', 'tdo_tag_rewrite' );
    function tdo_tag_rewrite() {
       if ( strpos( $_SERVER['REQUEST_URI'], '?tdo_tag=' ) ) {
          wp_redirect( str_replace( '?tdo_tag=' , '/tag/' , $_SERVER['REQUEST_URI'] ) );
          exit();
       }
    }

    That should do it. It’s quick and dirty but it works. I have a feeling that $_SERVER['REQUEST_URI'] needs to be sanitized, and using $_SERVER['REQUEST_URI'] makes me itch, but that worked for me.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Yep, using an unsanitized $_SERVER['REQUEST_URI'] is not safe.

    Use these lines instead. All it adds is esc_html() and sanitizes the output on the URL.

    <?php
    /*
    Plugin Name: TDO tag rewrite
    */
    
    add_action( 'template_redirect', 'tdo_tag_rewrite' );
    function tdo_tag_rewrite() {
       if ( strpos( $_SERVER['REQUEST_URI'], '?tdo_tag=' ) ) {
          wp_redirect( str_replace( '?tdo_tag=' , '/tag/' , esc_html( $_SERVER['REQUEST_URI'] ) ) );
          exit();
       }
    }
    Thread Starter Pierrick

    (@artpie)

    Brilliant Jan, it does work, the links are rewritten but throw the 404 error ??

    Thank you for your help again
    P

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Well that’s not unexpected. For it not to be a 404 both of these links would need to work.

    http://site-name/category/floppy-hair-beach/?tdo_tag=lama-jonny-2

    http://site-name/category/floppy-hair-beach/tag/lama-jonny-2

    The first will certainly work (I hope!) but the second wont work unless there is something to handle that request. Just changing it isn’t enough…

    Thread Starter Pierrick

    (@artpie)

    Hello,

    The first link does work, it is an actual link (where the site-name is replaced with a real url) and I have got a tag.php template which should be get picked up when the link gets rewritten hence I am puzzled here??

    Cheers
    P

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    What’s currently handling that ?tds_tag= parameter? That’s what needs to get the the new URL.

    Thread Starter Pierrick

    (@artpie)

    I see. Do you mean that whichever template handles ?tds_tag= parameter? needs to handle to rewritten url??

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    No no, I mean where does the ?tds_tag= come from? Is that handled by a plugin?

    For example, if I tag a post with Test the non fancy link looks like this: http://your-url-here/?tag=test.

    The plugin I gave you will handle the re-write, but still something has to handle the request.

    Thread Starter Pierrick

    (@artpie)

    Yep the plugin used is http://wordpress.org/extend/plugins/tdo-tag-fixes/ and it gets the tags from each category

    P

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Well, that explains it. I’ll take a look later on tonight.

    In the meanwhile you may want to contact the plugin author and see if they can handle those other URLs formatted the way you want.

    Thread Starter Pierrick

    (@artpie)

    It looks like this plugin is quite old but I have browse through the support archives but could not find anything related to my issue…

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Link rewriting’ is closed to new replies.