0

We had a major site rework and now our new site is PHP based. We have some sites which have our old pages linked to them which were asp pages in the past. I want to create a htaccess rule but I have failed to get the desired result. If anyone can help it would be great.

The incoming pages will be in the format

http://www.mysite.com/tour.asp?id=1234

where 1234 is dynamic and can vary. I want to redirect this to the page viewer.php and change the query string variable id to tourid so the page requested should be

http://www.mysite.com/viewer/1234

I have another rule in htaccess which routs the above url to

http://www.mysite.com/viewer.php?tourid=1234 (this is working)

I need a rule by which the 1st url is routed to the 2nd one hoping it will open the 3rd url. How can i achieve this?

Since I am new to htaccess, can someone help me around how to achieve this routing? I want to clear here that the server now is php and I have to accomplish this using htaccess only. I followed other similar questions but found none that answers my query.

1 Answer 1

1

UPADTE - I got it wrong first time, here's the corrected version.

# Match and capture the id value from the URL
RewriteCond %{QUERY_STRING} id=([0-9]+)
# Redirect by passing the ID to the new site - the ? at the end removes existing query string
RewriteRule tour.asp http://www.mysite.com/viewer/%1? [R=301,L]

I've assumed the IDs are numeric only.

The source for this answer is at https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html - search for "Modifying the Query String" to find the info about removing the query string.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks it works with a minor problem it adds the query string at the end like mysite.com/viewer/324?id=324
Try adding a ? just after the %1 - I'll update my answer now.
Thanks worked like a charm... Though irrelevant to the question asked here these rules will apply to all the requests with the querystring id? If yes any way I can restrict that to those only on tour.asp?
@LoneWOLFs The RewriteRule will only redirect requests that match tour.asp, and the RewriteCond just before it makes sure that requests have the id query string too
Oh Ok Thanks for the help. Accepted and upvoted. Specially for the querystring removal that was gr8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.