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.
LVL 3
Mr_SplashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_SplashAuthor Commented:
Perfect thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking

From novice to tech pro — start learning today.