• Resolved WildBil2Me

    (@wildbil2me)


    This is a tricky one I think. I was forced to switch between two different tag plugins because one is no longer maintained.

    The old plugin used ‘+’ in place of spaces in tags (so foo bar would be foo+bar). The new plugin uses ‘_’ (making it foo_bar). Because I’m sticking with the new plugin I think my best option is to redirect the old tag links to the new one.

    I’m figuring that I need a Rewrite rule in my .htaccess file that pushes ‘myurl.com/tag/foo+bar’ to ‘myurl.com/tag/foo_bar’ … anyone have any suggestions how to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • wouldnt be easier just to change the preg_replace line in the new plugin that uses _ to + ??

    Thread Starter WildBil2Me

    (@wildbil2me)

    I was thinking about that – but the thing is I’d need to replace it every single time I installed an updated version of that new plugin.

    uh, yeah, and? it’s one line ?? and plugins arent really upgraded that frequently. Up to you I spose.

    found via google, I tested it and it works for me:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule !\.(php)$ - [S=5]
    RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ tag/$1+$2+$3+$4+$5+$6+$7 [E=underscores:Yes]
    RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ tag/$1+$2+$3+$4+$5+$6 [E=underscores:Yes]
    RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ tag/$1+$2+$3+$4+$5 [E=underscores:Yes]
    RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)_(.*)$ tag/$1+$2+$3+$4 [E=underscores:Yes]
    RewriteRule ^tag/([^_]*)_([^_]*)_(.*)$ tag/$1+$2+$3 [E=underscores:Yes]
    RewriteRule ^tag/([^_]*)_(.*)$ tag/$1+$2 [E=underscores:Yes]
    
    RewriteCond %{ENV:underscores} ^Yes$
    RewriteRule (.*) https://www.YOURDOMAIN.com/$1 [R=301,L]
    Thread Starter WildBil2Me

    (@wildbil2me)

    With the plugin weighing in at over 5k lines of code across about 6 php files I have no way of guaranteeing that just searching for preg_replace will really do it. ??

    I knew that .htaccess would be a much quicker way of doing it I just was having trouble getting the syntax down. The solution I finally found was close to the one your suggested but a bit simpler. Here’s what I went with:

    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^tag/(.*)\+(.*)$ https://example.com/tag/$1-$2 [R=301,L]
    RewriteRule ^tag/(.*)_(.*)$ https://example.com/tag/$1-$2 [R=301,L]

    I only needed one RewriteRule because none of my tags are longer than two words. Once I got it figured out I added the last line to make everything more SEO friendly.

    In its current form the code above replaces both ‘+’ and ‘_’ with ‘-‘ making things pretty simple.

    Thanks for jumping in to help whooami, I appreciate it! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘.htaccess and replacing special characters’ is closed to new replies.