removing .php in ahref and still linking from old page

Viewed 26

I have an older PHP website where I've removed the .php extension from PHP files getting accessed and showing in the browser.

After I have added the relevant .htaccess parts and changed anchor's href from href="index.php" to href="index", is there any way to make the old one still work for SEO from pages that are for example indexed in Google?

1 Answers

is there any way to make the old one still work for SEO from pages that is for example indexed in google ?

Yes, this is essential if you are changing an existing URL format.

However, you do need to be careful of redirect loops, since you need to avoid redirecting the rewritten request that appends the .php extension.

You can do something like the following at the top of your .htaccess file, before the existing rewrite (after the RewriteEngine directive):

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)\.php$ /$1 [R=301,L]

The condition that checks the REDIRECT_STATUS environment variable avoids the redirect loop, since this is empty on the initial request from the client and set to "200" (as in 200 OK status) after the later rewrite.

You should test this first with a 302 (temporary) redirect to avoid potential caching issues.

Related