All seem to be working okay except the last entry that contains the first of the old site’s GET variables within the URL. If I type in the old url, I get a broken link warning even though the new path is valid, rather than being redirected as occurs with all the others.
I’d really appreciate anyone taking the time to point out what is hopefully a straightforward error in my file.
Hint: you can include the matched cat_id using %1 and the matched cat_name using %2
As for the second rules, you can’t do that using Apache alone, since Apache has no idea that business_id 1 belongs to Ecado-Ltd, business_id 2 belongs to Niche-Furniture, etc.
To overcome this you need to route these URL’s through a PHP script and let that PHP script redirect to the proper location.
Thank you - your much better alternative works a treat.
Sorry to push my luck but how would I tackle the rewrite rules when I have more than one variable to deal with in the query string i.e. also for cat_names, cat_ids as per the following:
However, when I also add the rewrite condition to pick up the cat_name variable (expecting them now to be %1 and %2) cat_name overwrites %1 and I lose the cat_id - there is no %2 i.e.
First of all, you’re using a screwdriver to drive a nail in the wall. Sure it works, but don’t you think a hammer (Redirect in this case) would be better?
As for the last entry, that doesn’t work because you can’t access the get parameters in a RewriteRule (nor in a Redirect), you need a combination of a RewriteCond and RewriteRule, like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} first_letter=([A-Z])
RewriteRule ^listings\\.php$ http://www.businessyork.com/listings/%1? [L,R=301]
Note that this rule automatically works for all characters A through Z, so there is no need to add more rules for those
The ? at the end of the rule is to prevent Apache from appending the query string to the new URL.
Hope that works for you and let me know if you have any questions
Sorry for being v slow on this but have been battling away for ages and still getting either internal server errors or broken links still.
Looking at your first reply I can see how the rewrite condition took the query string and the rewrite rule popped it back in with the %1 in the redirect path.
But when you have established additional variables, what is the syntax for controlling this - is it %2 %3 etc.
Been battling this for longer than I’d have cared to but a successful outcomes always takes the edge of things!
I’ve redesigned the site quite extensively and one of the purposes of the redesign was to make sure all the currently indexed pages redirect to the equivalent on the new site which is using search friendly urls via trackbacks hence the aboutus.php to /aboutus
Thanks to your assistance, hopefully I won’t lose any (more!) search ranking.
What you’re asking about is something I don’t particularly care for in mod_rewrite: The %n is only from the last statement processed. Therefore, you’ll need to ensure that both your key/value pairs are always present and in the same order (although I don’t understand why you really need both the id and name).
Note, though, that the earlier %2 becomes %3 in the RewriteRule.
For others wondering whether you can pick variables from multiple RewriteCond statements, yes, but you’d need to carefully craft the value/regex with the value/variable, i.e., repeat the %n from the previous statement and capture it again.
If I may go back to the top to ask a question, though, what is Apache supposed to serve with