Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2007-12-01 20:30:51
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
I can use some help with regular expressions...
So I’m switching from categories to tags. Now I need to redirect every possible URL with ‘category’ to either the tag with the same name, or, if that tag doesn’t exist, to the /tag/ page. I can do that like this:
Redirect 301 /category http://domain.com/tag
The tru_tags plugin redirects non-existing tags to /tag/. No problems so far.
BUT: I need URL’s like /category/existing-name/?pg=whatever
to go to /tag/existing-name/
.
And also /category/old-name/?pg=whatever
to /tag/
.
Or, if that would be simpler, /category/old-name/?pg=whatever
could go to /tag/new-name/
. (There are only five categories, so it wouldn’t be a problem to have separate rules for each of them.)
So I need either a RewriteRule or RedirectMatch, if this is even possible.
I tried (among lots of other things, and honestly, I searched and googled but I just don’t seem to get it…):
RedirectMatch 301 /category/(.*)/\?pg=d*$ http://domain.com/tag/$1
…but of course I don’t really know what I’m doing, so that doesn’t work at all :(
Any pointer in the right direction would be most welcome!
Offline
Re: I can use some help with regular expressions...
Without testing:
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5)/ http://domain.com/tag/
assuming old1 – old 5 are the old names.
Last edited by ruud (2007-12-01 22:35:53)
Offline
#3 2007-12-01 23:02:07
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: I can use some help with regular expressions...
Thanks Ruud, but the /category/old1/?pg=n is still being redirected to /tag/?pg=n. It does show the right page, because there is no pagination in that section, but I’d rather not have it in the URL, so it won’t be indexed by search engines.
Another question: /category/old1/ is now redirected to /tag/. But /category/old1 (without trailing slash) gives a 404. Is there a way to make the redirect work with and without the trailing slash?
Offline
Re: I can use some help with regular expressions...
I think you need mod_rewrite to get rid of the query string:
RewriteRule ^/category/(old1|old2|old3|old4|old5) http://domain.com/tag/? [R=301]
although, perhaps this trick also works with redirect match:
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5) http://domain.com/tag/?
Both deal with the slash issue as well, although they would match a category ‘old1andthensome’
Hmm… perhaps this:
RewriteRule ^/category/(old1|old2|old3|old4|old5)(/|\?|$) http://domain.com/tag/? [R=301]
or
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5)(/|\?|$) http://domain.com/tag/?
Last edited by ruud (2007-12-01 23:35:57)
Offline
#5 2007-12-01 23:38:08
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: I can use some help with regular expressions...
Almost there:
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5)$ http://domain.com/tag/$1
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5).+$ http://domain.com/tag/$1
will redirect as well /category/old1/ as /category/old1 to /tag/old1. However tru_tags creates URLs like /tag/old1/. Is it wrong if the URL of a page can be written both with and without a trailing slash?
And I still can’t get rid of the ?pg=n in the new URL.
Edit: hadn’t read your reply yet, will do that first…
Last edited by els (2007-12-01 23:38:57)
Offline
#6 2007-12-01 23:50:00
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: I can use some help with regular expressions...
ruud wrote:
RedirectMatch 301 ^/category/(old1|old2|old3|old4|old5)(/|\?|$) http://domain.com/tag/?
This one works, it redirects everthing from /category/old-name/?pg=n to /tag/. If what I had in mind is not possible, this is probably the next best thing to do.
So I think I will leave it like this. Thanks very much again, Ruud!
Offline
Re: I can use some help with regular expressions...
I thought that was what you had in mind… if you wanted something else, let’s hear it. Most things are possible :)
Offline
#8 2007-12-02 12:27:49
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: I can use some help with regular expressions...
Sorry if I was’n clear about what I had in mind. There were five old categories, with each of them lots of pages. Three of the category names do now exist as tags, two don’t. What I wanted to do is redirect like this for the first three:
- /category/old1/ -> /tag/old1/
- /category/old1/?pg=2 -> /tag/old1/
- /category/old1/?pg=10 -> /tag/old1/
and this for the last two:
- /category/old4/ -> /tag/
- /category/old4/?pg=2 -> /tag/
This last redirect could also be done the same way as the first one, because non-existing tags (/tag/old4/) will be redirected to /tag/ by the plugin anyway.
But it’s quite possible that the way you suggested, /category/*.* -> /tag/, is better, because visitors will notice sooner that the URLs have changed and they will find the tag they want anyway. But on the other hand, if they came from Google, they will probably find what they are looking for sooner if they land on the right tag list. I’m not so sure how to handle that.
Offline
#9 2007-12-02 13:01:53
- net-carver
- Archived Plugin Author
- Registered: 2006-03-08
- Posts: 1,648
Re: I can use some help with regular expressions...
Hello Els,
just bookmarking this mod_rewrite cheat sheet. A lot of other cheat sheets on that site that folks might find useful.
— Steve
Offline
Re: I can use some help with regular expressions...
RedirectMatch 301 ^/category/(old1|old2|old3)(/|\?|$) http://domain.com/tag/$1?
RedirectMatch 301 ^/category/(old4|old5)(/|\?|$) http://domain.com/tag/?
Offline
#11 2007-12-02 17:43:39
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: I can use some help with regular expressions...
Steve
Thanks, that’s a useful link!
Ruud
Thanks! It almost does what I want:) The only thing I think is wrong is that it adds a ?
after the URL. But if I leave out the ?
at the end it adds the ?pg=123
again.
Offline
Re: I can use some help with regular expressions...
I don’t know how to remove the ?
. Perhaps when using mod_rewrite, that can be done…
Offline