randyjensen
randyjensen

Reputation: 137

Remove .php extension after moving site to WordPress

I'm moving an old site from flat PHP files over to a new WordPress installation and want to make sure all the old URLs redirect properly. For example,

Old url: /va/apply.php should now go to: New url: /veterans-affairs/apply

I've got /va redirecting to /veterans-affairs properly, but cannot get the .php stripped from the URL.

I'm not sure if these needs to all be done in one step? I've tried everything I can find online and made as many tweaks as my limited knowledge in .htaccess has allowed.

This is also on WordPress, so there may be something I did that was conflicting with the pretty permalinks stuff there.

This is some of the code that I've tried among many others.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

This should redirect the user to the non-PHP location, but I keep getting a 404. This must be a combination of my code and WordPress' pretty permalinks.

Upvotes: 0

Views: 1734

Answers (2)

randyjensen
randyjensen

Reputation: 137

OK, I've finally got this working correctly. Again, what I'm trying to solve is to get this URL:

/va/apply.php

to correctly redirect to the new WordPress URL,

/veterans-affairs/apply

What worked for me was:

# This will remove the .php extension if it is not a directory, the file does not exist and it's not a WordPress specific admin page
RewriteCond %{REQUEST_URI} !/wp-(content|admin|includes)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# The basic redirect for /va
Redirect /va /veterans-affairs

I think what was breaking it was this final line that you find in all the examples:

RewriteRule ^([^/.]+)$ $1.php [L]

I think this was trying to actually resolve the URL before WordPress could do what it needed to do.

I also found this page which proved insightful

Hide .php Extension, Set Directory Index, Eliminate Duplicate Content, etc.

Upvotes: 0

Mike Wells
Mike Wells

Reputation: 426

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\.php$ $1 [L,QSA]

I have just had a quick look through where you are at and this above might help out. Add it to the wordpress htaccess above all the entries there so it can change this first... HTH

Upvotes: 0

Related Questions