I have a website that is not on ASP anymore, it's a Drupal 7 website now.

I need to use an .htaccess file to redirect with code 301 all pages with .asp extension to the 404 page or a specific URL.

(Background story: Drupal will catch non-existing pages such as domain.com/test and redirect to 404 page. But Drupal will not catch non-existing pages with ASP extension such as domain.com/test.asp and it will instead serve a generic Not Found Apache page.)

For example, if someone tries to access domain.com/test.asp, the user should be taken to domain.com/404

I have searched and searched but I can't find a solution to this particular problem. How can I code that in an .htaccess file?

  • "Drupal will not catch non-existing pages with ASP extension" - Why not? WHat is the difference between "does-not-exist" and "does-not-exist.asp"?
    – MrWhite
    Sep 10, 2013 at 23:55
  • not sure, if I go to /does-not-exist I get the Drupal 404 page. If I go to /does-not-exist.asp I get what seems to be an apache page Not Found The requested URL "/does-not-exist.asp" was not found on this server.
    – leonel
    Sep 11, 2013 at 15:03
  • Did this same server previously serve the ASP pages? Is it still configured for ASP? That might be the problem...(?) Try changing the Apache handler to send .asp files to PHP instead, or delete the handler altogether?
    – MrWhite
    Sep 13, 2013 at 10:52

2 Answers 2

RewriteEngine On
RewriteRule ^.+\.asp$ domain.com/404 [R=404,L]

Aughta do the trick :) It's pretty self-explanatory, it just rewrites every page that ends in .asp to your 404 page.

You should use 404 instead of 301 since you want to tell Google the page doesn't exist anymore. You can use 301, but then you'd have to redirect to a useful new page.

  • 1
    Just to note, you can't "redirect" to an alternative error document using the R=404 flag, the substitution string (domain.com/404) is ignored. The R=404 flag will serve the error document as specified by the ErrorDocument directive (or the server default). You might as well specify this with the pass-through (-) operator... RewriteRule .+\.asp$ - [R=404]. (The L flag is also unnecessary in this instance, although you might want to specify this if it helps readability.)
    – MrWhite
    Sep 12, 2013 at 16:59

Apache's mod_alias supports syntax for it without needing rewrite rules:

RedirectMatch permanent (.*)\.gif$ http://www.anotherserver.com$1.jpg

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.