Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
"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"? –  w3d Sep 10 '13 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 '13 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? –  w3d Sep 13 '13 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.

share|improve this answer
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.) –  w3d Sep 12 '13 at 16:59
    
My bad, I just took the 301 and edited it, assuming it would work. I shouldn't make assumptions... –  Ivo van der Veeken Sep 13 '13 at 10:11

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

RedirectMatch permanent (.*)\.gif$ http://www.anotherserver.com$1.jpg
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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