Redirect all .php pages excluding WordPress /wp-admin directory files

Redirect all .php pages

I ended up having 100s of crawl errors in Google Search Console for URLs that were ending in /blah.php and macariojames.com/dir/subdir/subdir2/blah.php for some reason.

Some even ended in .html, which was easy to resolve since WordPress doesn’t use .html files, but .php files.

After failing to write my own .htaccess RewriteRule and RewriteCond code, i found some useful code that i was able to tailor to fit my needs. I’ve shared it below. This code is specifically for Apache.

I know i could have let some of the links hit the default 404 Not Found error pages, but i wanted to redirect some pages that fit a certain criteria.

RewriteBase /
RewriteCond %{THE_REQUEST} \s(.+?)\.php\s [NC]
RewriteRule !^wp- %1 [R=301,L,NC]

Be sure you put the code in the root of your site — not in the root of your WordPress installation or it won’t do pages that don’t include the /blog. For example, on my website, i have macariojames.com/about and all the blog posts begin with /blog so by adding the above code to the .htaccess in the WP installation folder of /blog it will not rewrite say macariojames.com/about.php to macariojames.com/about as i want; but will have the page go to the default 404 page.

To prevent that, add the above code to your root directory making sure the first line is RewriteBase / or it still won’t redirect non /blog pages.

I hope that saves you time! I wasted maybe an hour scouring the web for a solution. If you come across anything screwy, let me know below.

Cheers and peace.

(Visited 109 times, 1 visits today)

If you found this post useful ...

Buy Me a Coffee logo
Wondering why you keep seeing lower-cased 'i' in my posts? Read -> Why ‘i’ is not capitalized
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x