0

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.

randyjensen
  • 127
  • 2
  • 10
  • 2
    possible duplicate of [Remove .php extension with .htaccess](http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – hjpotter92 Nov 13 '14 at 14:48
  • I've tried that code (and every other bit that I could find), but I still get a 404. I've tried it before the WordPress rewrites and after, but it doesn't seem to make any difference. – randyjensen Nov 13 '14 at 14:49
  • In that case post the code that you have tried. – Howli Nov 13 '14 at 14:56
  • Every variation of the code posted below. I think the issue is not the .htaccess code itself, but some conflict with WordPress. – randyjensen Nov 13 '14 at 16:51

2 Answers2

0
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

Mike Wells
  • 414
  • 4
  • 14
  • I've tried this and it doesn't work. I'm thinking that the .htaccess code that I've used was correct, but there is something conflicting with WordPress. – randyjensen Nov 13 '14 at 16:50
  • Yes of course, well spotted. If this was placed at the top of the htaccess it would have stopped all the following WP bits from working. Well done buddy, hope I 'helped' in some sort of way!! – Mike Wells Nov 14 '14 at 14:36
0

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.

randyjensen
  • 127
  • 2
  • 10