Skip to main content

Why is 301 Redirect Not Working After Removing .PHP Extension via HTACCESS? [Resolved]

I'm removing the .php extension from files contained within a specific directory on my website (courses) via .htaccess on Apache/2.2.26 (Unix).

Additionally, I'd like to 301 redirect the old .php version to non-php version.

Old URL Structure:

New URL Structure:

My Current Issues:

  • non .php version of the pages are not 301 redirecting.
  • both .php and non .php versions of the pages are viewable.

Here's my code:

RewriteEngine On

# External Routing
RewriteCond %{REQUEST_URI} (.*\/courses\/)([^\s]+)\.php [NC]
RewriteRule (.*courses\/)([^\s]+)\.php$1$2 [L,R=301]

# Internal Routing
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Question Credit: ju1985oh
Question Reference
Asked June 12, 2019
Posted Under: Network
1 Answers

Your Answer