How to 301 redirect old url with query string to the smarturl version

Hi

I’ve created smart urls in my .htaccess file using RewriteRule
e.g. www.sitename.com/variable1/variable2 now works

however if you type in the old url into a browser:
e.g. www.sitename.com/?variable1=x&variable2=y
The page loads and the old URL remains in the address bar.

So my question is, how can I type in the old url (with the query string), but that loads up the correct page AND changes the url in the address bar into the smarturl version?

I’ve tried doing a 301 Redirect however, after reading up I believe the ? is making the rule fall over.
e.g. Redirect 301 /?variable1=x&variable2=y /variable1/variable2

Thanks for any replies

Joe

I think this can be done, but it’s kinda silly, because users who type in the “real” (not “old”, you need to think of these as real and fake, not new and old) url (the one with the query string) will get redirected to a pretty url who, because he is not real, gets rewritten back to the real url. It’s a bit of a punishment for users and your server, I think, getting redirected around. Esp since, if they click on anything on this page, they get the fake urls from there on.

Instead, you spread your “pretty” urls around the web as much as possible. The point of a rewrite is really that you could change your url to something you liked, without a) changing the real original resource location or name and b) not breaking old backlinks.

In fact I believe what we did was add [L,R=301] to our rewrites to tell for example search engines that our rewrite was a permanent “new” url and to use the new pretty one from now on. This didn’t stop typing in the “real” url from working, but it meant pretty much everywhere, the new one was what users found.

But, if I were to try it anyway, I’d try this (I don’t know if it works with two vars and = and & in there)
RewriteCond %{QUERY_STRING} ^?variable1=x&variable2=y$
RewriteRule ^$ /variable1/variable2? [L] <– notice “^$” means there’s nothing between your domainname and the query string, so domainname.com/?querystring works, not domainname.com/something/?querystring

but the rewriteCond would have to be exact, unless the variable names are the same and you just want to change the numbers, you could (I think)
RewriteCond %{QUERY_STRING} ^variable(1)=x&variable(2)=y$
RewriteRule ^$ /variable%1/variable%2?

Again, this is kinda something pulled out of my butt so I haven’t tested it and I’ve only seen examples where there’s a single var in the query string, not multiple strung together with &'s, so may break horribly. I’m also not adding [L] after the rewrite since you need to rewrite that new url again back to the real one.

Normally with rewrites, query strings are kept (not rewritten) and tacked on to new urls… so I’ve got a ? at the end of the rewrite rule to set ? to “nothing”.

Hi Stomme poes

Thanks very much for the reply, i’ll give it a go

You mentioned:
“I think this can be done, but it’s kinda silly, because users who type in the “real” (not “old”, you need to think of these as real and fake, not new and old) url (the one with the query string) will get redirected to a pretty url who, because he is not real, gets rewritten back to the real url. It’s a bit of a punishment for users and your server, I think, getting redirected around. Esp since, if they click on anything on this page, they get the fake urls from there on.”

My reason for doing this is because of SEO… I’m getting duplicate content errors being flagged where sitename.com/1/2 is the same as sitename.com/?var=1&var=2, even though I’ve got rel=canonical set (I think correctly).
You can see an actual example here: http://www.slr-lens.co.uk/?type=canon&query=72&categoryId=31388
Also, both smart and non smart urls appear to be being picked up by the search engines, even though I only want the smarturl version to be listed. (Even though I’ve phased non smart urls out of the sitemap)…

Does this justify what I’m trying to do do you think?

Cheers

Joe

Joe,

While you’re attempting “loopy” coding, this same question had been addressed years ago. After thinking about my original “you can’t do that” response, I came up with a way to escape the loop (see the tutorial Article linked in my signature for the code - http://datakoncepts.com/seo#example-12). Subsequent to that, I discovered an Apache variable that made it even easier (which is the code shown) so you have the code for what you need.

Regards,

DK

DK: that hash doesn’t seem to exist.

Joe:

My reaseon for doing this is because of SEO… I’m getting duplicate content errors being flagged where sitename.com/1/2 is the same as sitename.com/?var=1&var=2, even though I’ve got rel=canonical set (I think correctly).

Ask yourself where they are getting these real urls. All the links on your page and all the links on external pages linking to you need to be your “pretty” urls. That’s why using [R=301] flags in your rewrite rules are important: it tells search engines (whether you have some proprietary non-standard google tag that they “claim” they consider a “strong hint” to the correct url, or not) that the “pretty” url is the “new, permanent” url. That means all your urls should be getting rewritten and SEs should NOT be offering the real/old urls in their indices. This is why most people aren’t trying to double-loop their urls when using mod_rewrite.

Go get someone who’s unfamiliar with your site to search for it in a search engine, click on whatever result has a pretty url, and then let them continue clicking through your site. Maybe all your urls are not getting rewritten. This is what we did to make sure our rewrites got all the types of pages: had the secretary click through them and we looked at the log of urls.

Where people sometimes have more trouble is when their urls have stuff like session information in them, or special codes like the user gets access to a special url because they have a coupon (even though another url brings everyone else to the same product, but different price), or you tack on that the user came via twitter or duckduckgo. You may specifically want to look for information regarding query strings since that’s the part of your url you’re worried about: the part after the ?

Mallory,

Bummer! It worked for me (FF 7.0.1) but, if it doesn’t work for you, try http://datakoncepts.com/seo.php#example-12.

Regards,

DK

DK… aha, I figured it out.

I turned on Javascript… and Javascript is indeed creating the id’s for the hashes. So don’t worry, it does work on old FF3.6 just fine; I just don’t surf with JS on by default (esp not for reading text-only pages).

You could refiggure that I suppose to leave the hash-id’s in there and just have JS do all the hide-showing stuff, but then, not sure how many non-JS users you’re actually getting who aren’t bots anyway : )

Mallory,

Aw, I thought that no-follow was set for signature links. Not worried about bots, though, as hits are not important.

Regards,

DK

Aw, I thought that no-follow was set for signature links

So far as I know, it is.