• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

301 redirect if URL contains .asp

Hi,

Can someone please tell me how I can perform a 301 redirect to my home page via a .htaccess file on the condition that the request URL contains '.asp'

Many thanks.
0
Mr_Splash
Asked:
Mr_Splash
  • 4
  • 3
1 Solution
 
Brad HoweDevOps ManagerCommented:
Hi,

I don't know what your index homepage is but here are some examples. create a .htaccess and place this in the directory you want the rules to work. These rules are set for the home directory so make sure the paths are appropriate.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\.asp$
RewriteRule ^(.*) /$1.html [R=301,L]

OR

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\.asp$
RewriteRule ^(.*) http:\\mydomain.com [R=301,L]

 
Cheers,
Hades666
0
 
Mr_SplashAuthor Commented:
Thanks hades666,

The latter solutions seems ideal can you confirm for me if this would allow for query strings i.e. file.asp?p=87

My regex knowledge is limited but am I correct is thinking removing the $ at the end of .asp would do this?
0
 
Brad HoweDevOps ManagerCommented:
Hi,

Slightly modified then. Give this a shot.

Then you can add the QSA option as well. QSA appends the original query string to the end of the rewritten URL. So if you requested http://www.mydomain.com/index.asp?p=87, and had the following rules:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\.asp$
RewriteRule ^(.*)\.asp$ http:\\mydomain.com [R=301,QSA,L]

Then the rewritten URL would be http://www.mydomain.com?p=87 

Hope it helps,
Hades666
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Mr_SplashAuthor Commented:
Thanks hades,

However I don't want to keep the query string, I just need to match any request URI that contains '.asp' anywhere (regardless of what's before or after). Then redirect to the homepage.
0
 
Brad HoweDevOps ManagerCommented:
In that case, same rule but remove the QSA.
Hades666
0
 
Brad HoweDevOps ManagerCommented:
Sorry about that typos. !!! Forwards slashes when they should be backwards and i added your string terminator :) ?.  This is the rule i just tested on my dev vm.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\.asp$
RewriteRule ^(.*)\.asp$ http://mydomain.com? [R=301L]

Takes *.asp or *.asp?querystrings and redirects to http://mydomain.com.

the ? at the end of the rule says make it explicit and don't carryover the querystring.

Cheers,
Hades666
0
 
Mr_SplashAuthor Commented:
Perfect thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now