The 301s are actually 302s in my tests so I don’t permanently send myself to the wrong place. The problem appears to be that item always matches, irrespective of whether category preceeds it or not. I’m not entirely sure how to deal with that as it’s never been a requirement before.
The question mark means ‘zero or one of the preceding character or range’
where (from their examples), the preceding character or range seems to be defined by being in brackets. I’m guessing that without brackets, it takes everything into account, so will be a universal match. If I’ve guessed right, what you actually want, instead of
But I freely admit that is off the top of my head, and take no responsibility for any catastrophes that may ensue if you just copy and paste this without testing it…[/font]
Good try! The ? in regex is “zero or one of the preceding character” but the square brackets you referenced could specify the character as being in an allowable set, i.e., it has nothing to do with grouping. Your use of the parenthetical does group and, if followed by a ? will make the entire group optional.
Antnee,
Redirect (almost) everything to one location, eh? Why not:
RewriteEngine on
RewriteRule ^(category/)?item(/|\\.html/)?$ category/item.html [R=301,L]
Okay, that looks a bit complex but it says:
Start with an optional category/, continue with item, continue with EITHER an optional trailing / OR .html/ and ends. The redirection will not loop as category/item.html does not have a trailing /.
WARNING, a redirection like this (many different levels of directories for your request) will force the use of (internal) absolute links in all support files (.css, .js, .gif, .jpg, etc.) and, IMHO, a trailing / is something which is ONLY done for a directory request.
Feeling a bit daft actually. I knew that about the ? but I still put it in my example, though admittedly I was trying it with and without and it just happened that I had it in at the time. Even so, pointless!
@dklynn I tried your example and had to make a slight change to make the redirected URL the full URL:
That did the job perfectly and also got around another problem I was having further down where another part of the rewrites were appending query string data to the URL so it was becoming:
Er … huh? The only square brackets that I refered to or featured in my post were the control codes (I don’t know if there’s a more technical name for them than that) at the end of the lines…
What square bracket comment? It must have been in another thread but it was not aimed at you; it referred to that OP’s attempting to match Google’s encoding of the ?, = and & characters within regex expressions.
Antnee,
You specified the series of item, item/, item.html/, category/item, category/item/ and category/item.html/ so that’s EXACTLY what you got! Okay, okay, a change in specification brings:
RewriteEngine on
RewriteRule ^(item\\.html|(category/)?item(/|\\.html/))?$ category/item.html [R=301,L]