2

I am taking over a site that was built in ASP and provides an endpoint for a bunch of third party apps. We are moving to a PHP based site, but we have to account for these end points.

Basically on the only site it might make a request like test.asp?id=31231312&qs=423434AFSDF434 and we want to route this to our own test.php?id=31231312&qs=423434AFSDF434 and then we can do the rest. Is there a way to set up a 404 redirect to catch this and route properly in Apache? Any other ideas of an easy way to route it?

Thanks in advance.

jeremykrall
  • 181
  • 1
  • 15

1 Answers1

1

I doubt you want a 404 (not found)? You probably want a 301 (permanent redirect)?

First make sure the mod_rewrite module is enabled, then stick this in a .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^test.asp$ test.php [R=301]

If you're looking to redirect ANY asp to its php counterpart, then:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*).asp$ $1.php [R=301]

Examples as per comments:

Rule for specific file in a folder:

RewriteRule ^folder1/test.asp$ folder1/test.php [R=301]

Redirect different asp files to the same php file:

RewriteRule ^test.asp$ test.php [R=301]
RewriteRule ^test2.asp$ test.php [R=301]
RewriteRule ^folder1/test.asp$ test.php [R=301]

Unsure if needed but... redirect ALL asp files to a single php file:

RewriteRule ^.*.asp$ test.php [R=301]
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • How will this handle path names? For example if we have a folder structure with folder1/test.php folder2/test2.php folder3/test1.php, etc. Do we need to mirror all of those? Is there a way to direct any of the ASP pages to a single PHP and maintain the querystring? Thanks! – jeremykrall Dec 04 '14 at 22:19
  • As long as you put the htaccess file in the root folder, the second example will handle all files in all subfolders just fine (assuming the php file is in the same folder as the asp!). The first example is specific to a single file, so you'd need to edit the last line with the correct paths. If you want to redirect several asp files to one php file, again use the first example and repeat the last line for each file. Note that both examples retain the query string, this is default behaviour. – rjdown Dec 04 '14 at 22:44
  • Thanks, I just tried this and when I hit a sample URL I just get a 404. To confirm, mod_rewrite seems to be enabled and this is a fresh install so nothing on here to interfere with. I literally only have the .htaccess file a sample PHP file in the root folder. Any ideas? – jeremykrall Dec 04 '14 at 23:22
  • Have a look at this question http://stackoverflow.com/q/12202387/1301076 (a simple way to test if htaccess files aren't working is to type a load of nonsense into them... you should get an internal server error) – rjdown Dec 04 '14 at 23:35