Forum Moderators: phranque
The old format was 'myurl.com/tag/foo+bar' and the new format is 'myurl.com/tag/foo_bar'
I'd like to redirect any incoming links to the new format but I've been having trouble finding .htaccess documentation that really covers this. I assume that the best option would be to rewrite the URL by replacing the '+' signs with the '_' signs. I'm just unsure how to proceed ...
Any suggestions?
The following code will redirect requested URLs containing three, two, or one underscore to the same URL, but with underscores replaced with hyphens.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^_]+)_([^_]+)_([^_]+)_([^.]+)\.html$ http://www.example.com/$1-$2-$3-$4.html [R=301,L]
RewriteRule ^([^_]+)_([^_]+)_([^.]+)\.html$ http://www.example.com/$1-$2-$3.html [R=301,L]
RewriteRule ^([^_]+)_([^.]+)\.html$ http://www.example.com/$1-$2.html [R=301,L]
Here's the first rule modified to replace three "+" characters with hyphens as an example:
RewriteRule ^([^+]+)\+([^+]+)\+([^+]+)\+([^.]+)\.html$ http://www.example.com/$1-$2-$3-$4.html [R=301,L]
These rules will likely need to be further adapted, as I'm not sure what your real links look like. For example, I assumed a ".html" extension on your links, which may or may not be the case. If there is never an extension, then you could/should replace "([^.]+)" with "(.*)".
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
I swapped to the '-' rather than the '_' and your code is working great for handling those redirects. (this was an existing option in the plugin.)
It isn't redirecting when I substitute the '+' sign though.
What I mean is that right now if I type 'http://example.com/tag/Foo_bar' it will direct to 'http://example.com/tag/Foo-bar' but 'http://example.com/tag/Foo+bar' still goes to a 404 page.
I should include the edited code I'm using:
RewriteRule ^tag/([^_]+)_([^.]+)$ http://example.com/tag/$1-$2 [R=301,L]
Solution
Thanks jdMorgan, I figured it out. It took me a few minutes to really understand the situation. I'm using the following two lines and they're doing exactly what I need.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^tag/(.*)_(.*)$ http://example.com/tag/$1-$2 [R=301,L]
RewriteRule ^tag/(.*)\+(.*)$ http://example.com/tag/$1-$2 [R=301,L]
[edited by: WildBil2Me at 5:19 pm (utc) on Mar. 25, 2007]