Eric
Eric

Reputation: 429

Redirecting all URLs that end in .php to no extension

I've recently removed the .php extension from all of my pages.

Google results are still showing:

www.mysite.com/page.php
www.mysite.com/directory/page-example.php

These are now dead links. The new ones are:

www.mysite.com/page
www.mysite.com/directory/page-example

What would be the appropriate redirect so that if someone clicks one of the .php URLs, they are redirected to the extension-less URL

Upvotes: 3

Views: 5757

Answers (1)

LazyOne
LazyOne

Reputation: 165088

  1. Use <link rel="canonical" href="FULL_PROPER_URL" /> in your web pages. This will tell Search Engine to use that URL when displaying search results and will treat this a s main URL when seeing duplicate URLs. Details are here:

  2. Use 301 Permanent Redirect. Please note that this WILL NOT stop Google or any other search engine to requesting .php pages if such link is publicly available on some other site.

This needs to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]

The above rule will do 301 redirect to a php-less URL. It will only redirect if .php file was requested directly and will not touch already rewritten URLs.

Upvotes: 7

Related Questions