Help needed with 301 redirect in htaccess

Well, I have read and read and I still dont get it - hence this act of last resort - Post to a SitePoint Forum…
Here is what I need to achieve in a nutshell:

I need to redirect:
http://www.oldsite.com/accommodation?action=some_value&agent=something&id=9999
to
http://www.newsite.com/stay<same-long-query-string>

I have taken the time to read DK’s very comprehensive tutorial for which I am greatful, and have searched elsewhere for clues - but - I am still clueless…

So, any help would be much appreciated.

Thanks.

JA,

You didn’t read the early bit in mod_rewrite REGEX colored red: Note: YOU must create the URI links in your new format; mod_rewrite’s job is to rewrite it to something Apache can serve to your visitors!.

Saying that, you actually want to redirect BACK to accommodation (no extension?) with the query string.

Wouldn’t

# .htaccess in DocumentRoot of oldsite
RewriteEngine on
RewriteRule accommodation http://www.newsite.com/stay [R=301,L]

… work for this? Where’s the query string? It goes along for the ride automatically (unless you create a new query string or specifically kill it).

Off Topic:

If you’re using extensionless filenames, how are you getting your server to serve files with extensions?

Regards,

DK

Hi DK - and many thanks for your response.

It never ceases to amaze me that in this field of endeavour we are all constantly learning and no one person can know it all. The other thing that also amazes me is that when I make a mistake, I can keep making that same mistake. Last night I was relieved to find that I am not alone in this when I came across a situation in another forum where someone kept going down the same rat hole and it is not until another pair of eyes appear on the scene that the error becomes a glaring beacon!

So, to begin, let me come to the OFF TOPIC question: The sites concerned are built with PyroCMS which is based on Codeigniter. So, no filenames are present in the URI as the various controllers in the MVC structure manage the routing of requests and responses.

Next - the mistake I kept repeating was that I kept considering that /accommodation plus the query string, were the URI. On reading your reponse DK, I realised this is an error and that there is the URI (eg: accommodation on one hand and stay on the other), and attached to the URI is a different animal called a query string. So, thanks for the jog!

OK, so now, some more details are called for so I apologise in advance for not revealing the full extent of what we need to accomplish here (if that is at all possible).

The business concerned has gone through a re-branding process - hence the new site. In addition to this, both sites rely on a 3rd party provider for some of the data that gets presented on the sites. This data is pulled in via iFrames.

Now, to further complicate matters, the retrieval of this data from the other provider takes time. It was decided by the client that on the new site, instead of dragging this data in, they would have their own local copy of some of that data - which meant double handling, but provides a better experience for the end user.

At oldsite.com/accommodation a listing of all accommodation establishments gets pulled in via the iFrame as the default process.

However, at newsite.com/accommodation, the local copy of the data gets presented without the need to resort to the iFrame.

The other thing to consider is that there is still a need to be able to provide detailed information from the other provider and that is being done via the query string appended to /accomodation on the oldsite.com. I have no idea how this can happen, all I know is that it does.

With the introduction of the new site, it was decided to use an alternative address for the delivery of the detailed information, hence the /stay uri has been adopted for that and this now is the address used for the iFrame data…

So, the critical thing here is to be able to redirect visits to the oldsite.com/accommodation-plus the query string - to newsite/stay-plus the query string. If users visit oldsite.com/accommodation - we do not want them to be re-directed to newsite.com/stay. In fact, the accommodation nav links on oldsite.com have been changed in the CMS so that they now point to newsite.com/accommodation.

I hope all this is making sense. Thanks for taking the time to read and offer your suggestions/conclusions. :slight_smile:

Hi John!

Now that was a detailed explanation! The only missing part was confirmation that you’re on an Apache server (where mod_rewrite is native).

The Off Topic part of my reply was to suggest that you must either know some mod_rewrite OR have altered your type handler on the server. I certainly didn’t expect another tool (which may make mod_rewrite irrelevant).

The single important point of your post, however, was that you REQUIRE the presence of a query string for the redirection to stay (with the query string). That’s pretty simple:

# .htaccess in DocumentRoot of oldsite
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule accommodation http://www.newsite.com/stay [R=301,L]

Thought (yes, I do get them every once in a while :shifty: ): If you’re waiting for input from another source, AJAX might provide a means to load (and display) YOUR page while fetching the outsourced data (which would appear as soon as it’s loaded - and could replace a “keep your shorts on, I’m fetching your data” message or similar image) and allow you full control of your page/site at the same time without significant maintenance issues. I do that with a database search which refreshes a table of tide times at http://mercurybay.co.nz/.

Regards,

DK

Thanks DK for your time and your input. Much appreciated I can tell you as your jog helped me sort out where I was going wrong - I was just over-doing the whole thing and the solution is so darn simple its - well embarrassing.

Take care.

John,

Don’t be embarrassed! It’s a matter of understanding the little bit of regex and being able to define the problem … then looking for the typical errors that experience allows you to do after a short time.

Regards,

DK

Many thanks David. The truth be told, this is only the 2nd time I have needed to do 301 redirects in 10yrs at this stuff !!
You d’ man DK !

:D:D:D

Thanks, John. I needed a little help with mod_rewrite years ago and rapidly became SitePoint’s expert - before being on staff. Years of answering the same questions got rather exasperating (and wasn’t helped much by creating a tutorial and linking to my own site in my signature or turning it into sticky threads) so I’m “relaxing” in “member mode.” The point is that solving your own problem brings good experience but helping others through their problems broadened my expertise significantly. My point in being here is to help others by sharing that information - so please help others when you see the opportunity, too.

Oh, my: Another soapbox! Sorry.

Regards,

DK