ReWriting URLs W/Different Query Strings Differently In htaccess

I am building a new site but my old site’s links have been picked up by search engines and other places. When building this new site, we changed the structure based on feedback.

For example, I used the 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]

The above code worked to ensure that greatexperiencesbaltimore.com/contact.php and greatexperiencesbaltimore.com/contact.php?page=catering both redirect to eventplanningcatering.com/contact-us/

The problem is I have other scenarios where I need:

greatexperiencesbaltimore.com/catering.php to redirect to eventplanningcatering.com/catering/
greatexperiencesbaltimore.com/catering.php?page=wed to redirect to eventplanningcatering.com/wedding/
greatexperiencesbaltimore.com/catering.php?page=corp to redirect to eventplanningcatering.com/corporate-events/

There are actually more than just the above 3 variables. How do I accomplish this in my .htaccess file?

I am fairly new at this. :confused:

Thanks, Nick

Hi Nick!

Welcome to SitePoint!

I believe I should refer you to the mod_rewrite tutorial in my signature as I would otherwise whine at you to recommend that you combine the two RewriteCond statements (but ONLY if you share this domain with other domains - otherwise, they are superfluous) then suggest a “get real” with the quotes and escaping of the redirection (neither of which is required … or desirable).

In the tutorial, look at the example code dealing with query strings as those snippets should help.

Regards,

DK

David,

I looked at your tutorial and I think I was starting to understand. This is all very real to me remember. I tried to insert it and it doesn’t quite work right.

Here is what I entered:


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/[B]corporate-events[/B]/ (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/[B]wedding[/B]/ - instead it redirects to http://www.eventplanningcatering.com/[B]corporate-events[/B]/ as well (which is incorrect)

Could you provide any guidance here?

Nick

Nick,

I think you missed one of the most important aspects of the tutorial (probably not sufficient emphasis on my part): Start with a specification of the redirections you’re attempting.

Are you attempting to force www on greatexperiencesbaltimore.com?

OMG! PLEASE STOP ESCAPING CHARACTERS IN THE REDIRECTION!

You’ve stated:

The above code worked to ensure that greatexperiencesbaltimore.com/contact.php and greatexperiencesbaltimore.com/contact.php?page=catering both redirect to eventplanningcatering.com/contact-us/

The problem is I have other scenarios where I need:

greatexperiencesbaltimore.com/catering.php to redirect to eventplanningcatering.com/catering/
greatexperiencesbaltimore.com/catering.php?page=wed to redirect to eventplanningcatering.com/wedding/
greatexperiencesbaltimore.com/catering.php?page=corp to redirect to eventplanningcatering.com/corporate-events/

Why not (because you didn’t answer about sharing the physical webspace of greatexperiencebaltimore.com and eventplanningcatering.com, I’ll assume that they do NOT share the same physical space):

# .htaccess in greatexperiencebaltimore.com's DocumentRoot

RewriteEngine on

RewriteRule ^contact\\.php$ http://eventplanningcatering.com/about-us/ [L]

RewriteCond %{QUERY_STRING} page=wed
RewriteRule ^catering\\.php$ http://eventplanningcatering.com/wedding/ [L]

RewriteCond %{QUERY_STRING} page=corp
RewriteRule ^catering\\.php$ http://eventplanningcatering.com/corporate-events/ [L]

RewriteRule ^catering\\.php$ http://eventplanningcatering.com/catering/ [L]

Please have another read of the tutorial.

Regards,

DK

Sorry, I was told once that i had to put the \ in before punctuation, which is what i think you are referring to as “escaping” - again, very new at this.

The two websites are in the same hosting account. GreatExperiencesBaltimore.com was a standalone website until 72 hours ago. Now it is an add on domain to EventPlanningCatering.com which is the new website. However, the old site had gained ranking and is linked to from other sites so even though it is no longer a live site, I wanted to make sure all of its old links redirected to the equivalent page on the new site.

My goals are simple:

  1. If someone types in www.greatexperiencesbaltimore –> redirect
  2. If someone types in http://greatexperiencesbaltimore –> redirect
  3. On URLs from old site with query strings, I want each query to redirect to a certain page (regardless of whether www or http was used)

With my previous knowledge and the help of your tutorial and your last code, i came up with the following which seems to be working well (not that i fully understand why). I believe the first two conditions tell it to do the redirect whether www or http is used. The 3rd condition is query string, and the fourth being the Rule. I used your tutorial to figure out the very first set where i had to specify when no query string was added. That seems like it works too. So, I think my problem is solved. Just for a quick confirm, that looks right to you too?:


RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\\.greatexperiencesbaltimore\\.com$
RewriteCond %{QUERY_STRING} !page=
RewriteRule ^catering\\.php$ "http://www.eventplanningcatering.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.eventplanningcatering.com/corporate-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.eventplanningcatering.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.eventplanningcatering.com/private-event/bar-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.eventplanningcatering.com/event-venues-locations/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.eventplanningcatering.com/private-event/?" [R=301,L]

Nick,

Whoever told you that was … ah, wrong!

Specificity:

List the (functional) requirements for your redirections:

Redirect EVERYTHING from greatexperiencesbaltimore to eventplanningcatering? Why? If it’s a addon domain for EPC, keep it running! To me, it’s illogical (to me) to combine the two domains as I’d think that their content would be very different.

That said, the query string redirections should become moot.

While I’m not a fan of what it appears that you’re doing, let’s get past that to your “specifications”:

  1. If someone types in www.greatexperiencesbaltimore –> redirect

Redirect where?

  1. If someone types in http://greatexperiencesbaltimore –> redirect

Redirect where?

  1. On URLs from old site with query strings, I want each query to redirect to a certain page (regardless of whether www or http was used)

List of key/value pairs and their redirections?

WHY do you repeat the test on the domain for every request? Functionally, that only needs to be done once (then the query string redirections would be managed from EPC’s .htaccess (different DocumentRoot?).


RewriteCond %{HTTP_HOST} ^greatexperiencesbaltimore\\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\\.greatexperiencesbaltimore\\.com$
RewriteCond %{QUERY_STRING} !page=
RewriteRule ^catering\\.php$ "http://www.eventplanningcatering.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.eventplanningcatering.com/corporate-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.eventplanningcatering.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.eventplanningcatering.com/private-event/bar-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.eventplanningcatering.com/event-venues-locations/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.eventplanningcatering.com/private-event/?" [R=301,L]

Well, I’ve beaten the dead horse about the domains sufficiently except to say that, if this is in the GEB DocumentRoot’s .htaccess, each and every RewriteCond addressing the {HTTP_HOST} is not required (you’re getting the external redirect in every case based on the URI and query string value for page). Personally, I’d tighten-up the code on the query string values to ^page={value}$ unless you have something else in the query string with page in which case I’d use ^(.&)?page={value}(&.)$ to select only the page={value} from the string. Note that {value} represents the eth, off, mitz, wed and corp values.

Sorry if I got overly pedantic with you but your answer is for you AND other members who look at your threat so I need to get it technically correct and offer techniques for improvement.

Regards,

DK