Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Rewrite Urls With Different Query Strings To Different Destinations - Htaccess

htaccess mod_rewrite query strings redirect string

  • Please log in to reply
5 replies to this topic

#1 NickJAB

NickJAB

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:PHP, Others
  • Learning:Objective-C, JavaScript, Others

Posted 13 June 2012 - 07:23 AM

Hello, this is my first post here.

I recently just built a brand new website and I want to redirect individual pages from my old site to the new one. Primarily for user experience but also to transfer search ranking from my old site to my new one. I am using .htaccess mod_rewrite to do this. The old site was not built by me and I am now finding out that many of the URLs have query strings.

In some cases, I want to simply drop all query strings for a URL and have them all direct to the same place. To do this, I used the following code:

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteRule ^contact\.php*$ "http\:\/\/www\.eventplanningcatering\.com\/contact\-us\/?" [R=301,L]

This seems to work perfectly to ensure that OldSite.com/contact.php, OldSite.com/contact.php?page=catering, and OldSite.com/contact.php?page=birthday all redirect to NewSite.com/contact-us/

So here is where I am running into a problem. I have numerous scenarios where I need:

OldSite.com/catering.php to redirect to NewSite.com/catering/
OldSite.com/catering.php?page=wed to redirect to NewSite.com/wedding/
OldSite.com/catering.php?page=corp to redirect to NewSite.com/corporate-events/
OldSite.com/catering.php?page=bday to redirect to NewSite.com/birthday/

I have tried researching and looking for the answers for an entire day now and I am at a loss.

Could someone please assist me in figuring this out, or pointing me in the right direction as to what to do in my .htaccess file to accomplish this?

Thank you

  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 13 June 2012 - 08:57 AM

I think the easiest way would be to redirect everything to your index.php.
and in php/mysql/whatever database create a list of urls that should be directed and to what address and make a header() redirect with a 301 message if one of those urls were hit, while new urls are served normally.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 NickJAB

NickJAB

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:PHP, Others
  • Learning:Objective-C, JavaScript, Others

Posted 13 June 2012 - 07:36 PM

I am completely unfamiliar with doing that. Can you tell me if that is an effective method for SEO purposes? Also, can you point me in the right direction for learning how to do that?

And I assume by your response that there is no way to do that in htaccess?

Thank you for your help.

Nick
  • 0

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 14 June 2012 - 12:57 AM

You do it both in index.php and in .htacceess, in htaccess, you put the main redirect for everything and in index.php you handle everything else.

of course you can do it in .htaccess, but it requires a line for each redirect there, with my suggestion, you can do one redirect in .htaccess and do the rest in php.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#5 NickJAB

NickJAB

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:PHP, Others
  • Learning:Objective-C, JavaScript, Others

Posted 14 June 2012 - 09:44 AM

My problem (or at least my problem because this is beyond my expertise) is that the site I am redirecting to is a Wordpress site and I was hoping to avoid modifying the Wordpress files significantly.

I did try one more thing that I found in htaccess:

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} !uniquekey=corp
RewriteRule ^catering\.php$ "http\:\/\/www\.eventplanningcatering\.com\/corporate\-events\/?" [R=301,L]


RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} !uniquekey=wed
RewriteRule ^catering\.php$ "http\:\/\/www\.eventplanningcatering\.com\/wedding\/?" [R=301,L]


The problem is it is only partially correct in function.

greatexperiencesbaltimore.com/catering.php?page=corp does redirect correctly to http://www.eventplanningcatering.com/corporate-events/ (it goes to the right place AND drops the query string which i want to happen).

however,

greatexperiencesbaltimore.com/catering.php?page=wed does not redirect to http://www.eventplanningcatering.com/wedding/ - instead it redirects to http://www.eventplanningcatering.com/corporate-events/ as well (which is incorrect)

So, I guess here is my question... if you recommend using the PHP method, how do i go about doing that. If however, I am going to stick to htaccess and write a line for each redirect (there are not that many of them), what is wrong with my code?

Thank you so much for your help. My head is splitting from trying to figure this out for 3 days now. LOL
  • 0

#6 NickJAB

NickJAB

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:PHP, Others
  • Learning:Objective-C, JavaScript, Others

Posted 15 June 2012 - 10:17 AM

Thank you for your help. Since I didn't know how to do what you were referring to in PHP, I went ahead and solved the problem in htaccess. Below is my solution which seems to work perfectly for what I wanted. In case anyone ever has the same problem as I did, I hope this solution helps:


RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} !page=
RewriteRule ^catering\.php$ "http://www.eventplan....com/catering/" [R=301,L]

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} page=corp
RewriteRule ^catering\.php$ "http://www.eventplan...orate-events/?" [R=301,L]

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} page=wed
RewriteRule ^catering\.php$ "http://www.eventplan....com/wedding/?" [R=301,L]

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} page=mitz
RewriteRule ^catering\.php$ "http://www.eventplan...-bat-mitzvah/?" [R=301,L]

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} page=off
RewriteRule ^catering\.php$ "http://www.eventplan.../off-premise/?" [R=301,L]

RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.greatexperiencesbaltimore\.com$
RewriteCond %{QUERY_STRING} page=eth
RewriteRule ^catering\.php$ "http://www.eventplan...rivate-event/?" [R=301,L]

Thanks for your help!
  • 0





Also tagged with one or more of these keywords: htaccess, mod_rewrite, query strings, redirect, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download