URL rewrite - Dealing with the old urls

Hi All,

I am just in the process of implementing seo urls on my site (iis + isapi 3) and I have it working a treat using…

RewriteRule ^user/([^/])$ /profile.cfm?name=$1 [L]
RewriteRule ^user/([^/]
)/([^/]*)$ /profile.cfm?name=$1&nomobile=$2 [L]

now I have 2 questions.

  1. Can I combine the 2 rules above into 1 more efficient rule? the 2nd picks up an optional extra url variable
  2. How should I best deal with the old urls - these are out there and indexed by Google already - I need to deal with people who may use the old one? My thoughts are that if i implement an on page 301 it will get into an infinite loop of redirecting it to itself



bb (Mike?),

IIS does have a subset of mod_rewrite available but different versions of IIS handle mod_rewrite type directives VERY differently.

Now, to answer your questions:

  1. Yes. the second atom (and preceding /) can be optional so the nomobile value may be null - hidden, that’s not a problem.

  2. It depends upon how smart you were with your conversion. If you retained the same value for name & nomobile, mod_rewrite can do that in a flash! Otherwise, a RewriteMap is required. Since most people don’t have access to the server configuration files (RewriteMap errors can bring the entire server down), a “redirection handler” script is just as good.



Thanks DK! is it just a case of adding a ? for the optional part?

With regards to the name it is unfortunately changed from a user_id to a user_name so I think I may need what you say - do you have a link for more info on a “redirection handler” script? I have searched google and not sure what Im looking for.

In terms of 301 redirecting - is this what it will do? I have updated all links on site and submitted a new sitemap and im in the process of adding a rel canonical tag too to help.

Thanks for your time :slight_smile:

Hi Mike,

Yes, the ? (IN REGEX) matches the preceding character (or group of characters) 0 or 1 time, i.e., optional. Obviously, group characters to ensure a match of more than one character and put the ? outside the closing parenthetical. I think I have a better explanation in the Regex section of my signature’s tutorial.

Changing from user_id to user_name isn’t a problem unless you’ve changed the values, too. Typically, that IS the problem.

Handler file? It depends upon the complexity of your redirection … OR NOT! Check http://wilderness-wally.com and look at the links in the TOC on the left of every page. They’re created using the title of each article, not the id. I could just as easily duplicate the script to take the id of the articles but the numbers contain no useful information (this is a client-maintained website so I’ve had to limit the characters Mr Wilderness can use in the titles - URI restrictions, of course - and perform my redirections based on “user_name” rather than “user_id”.

If you’ve not changed the database, you could have your profile.cfm look for the two keys and use the value of the one provided to access the database. Trivial alteration, IMHO (if-then-else).

If you’ve changed the database names, though, you’re in deep doo-doo because you’ll have to create a handler script to do a lookup and redirection for you. I’ve created a RewriteMap for a “casual client” which performs this database lookup and redirection while in the .htaccess file but it could just as easily perform this task as the first thing it does in your generic script, too (albeit, a bit more complicated than the if-then-else above).

More information is required for an opinion on which way to go, though, but I believe you’ve got enough to handle the problem now anyway. Oh, well, you know where to find me if you need more information (like my collection of URI-acceptable characters).



HI DK - thanks again for taking the time to reply :slight_smile:

I have literally switched from using the user_id to user_name which already existed in the url - so that is fine - I will add in the if statement you suggest as this will stop the page breaking.

I guess what I mean more though is that at the moment im doing this…

when i type:


it rewrites correctly http://www.mysite.com/profile.cfm?name=mike

what im concerned about (with Google) is that the “old” url is still available at http://www.mysite.com/profile.cfm?name=mike - what is the best way to deal with that? all of the old links on site have been replaced but the wider internet knows of them - how should i deal with that?

Oh, Mike, you didn’t read my signature’s tutorial nor view the “Loopy Redirect” (Redirect TO New Format) section within the sample code, did you? It’s all there.