Mani5556
Mani5556

Reputation: 402

301 to extensionless page only if it exists

If a user goes to:

www.example.com/mypage.asp 

I want it to redirect to an extensionless version of the page:

www.example.com/mypage 

But I only want to do it if the requested page exists, so:

www.example.com/mypage.asp -> 301 to -> www.example.com/mypage
But www.example.com/notapage.asp -> 404

How best to do this in .htaccess?

Upvotes: 1

Views: 36

Answers (1)

anubhava
anubhava

Reputation: 786291

You may try this rule in your site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/+(.+?)\.asp[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} -f will make sure that it is done for a an existing file only.

Upvotes: 2

Related Questions