Forums

Link rewriting (14 posts)

  1. Pierrick
    Member
    Posted 4 months ago #

    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

  2. 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.

  3. Pierrick
    Member
    Posted 3 months ago #

    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

  4. 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.

  5. 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();
       }
    }
  6. Pierrick
    Member
    Posted 3 months ago #

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

    Thank you for your help again
    P

  7. 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...

  8. Pierrick
    Member
    Posted 3 months ago #

    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

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

  10. Pierrick
    Member
    Posted 3 months ago #

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

  11. 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.

  12. Pierrick
    Member
    Posted 3 months ago #

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

    P

  13. 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.

  14. Pierrick
    Member
    Posted 3 months ago #

    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...

Reply

You must log in to post.

About this Topic