1

There are tons of posts regarding "remove PHP extensions." I've tried a bunch. Some work... some don't... but none do what I need. [Note: this was marked as a "duplicate" ... yet noone can provide me with a link to a solution here]

The code below allows my users to go to a page, and view it WITHOUT the php extension. However, I need the users to be redirected from the URL with an extension, to the no-extension URL ... Right now, going to "page.php" lets them view the URL as www.website.com/page.php (they need to see www.website.com/page).

Right now:

GO TO: www.website.com/page
--- URL IS: www.website.com/page   
GO TO: www.website.com/page.php
--- URL IS: www.website.com/page.php  
--- NEEDS TO BE : www.website.com/page

Code is below:

RewriteEngine on

# remove php ext
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# Add www to any URLs that do not have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Someone said this was a duplicate post where the solution was:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

That didn't work. It does exactly what my code does above.


Found the following that worked...

RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
  • starkeen, your "duplicate post" didn't work. Please don't be so quick to invalidate a post. –  May 08 '18 at 15:31

1 Answers1

0

However, I need the users to be redirected from the URL with an extension, to the no-extension URL

.htaccess approach

Give this a try:

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

(Credit: How to remove .php, .html, .htm extensions with .htaccess)

Non .htaccess approach

If you do not have a lot of pages, another approach would be PHP way, with a simple header redirect...

In other words, at the top of the page, simply do something like--

<?php
if(strpos(basename($_SERVER['PHP_SELF']), ".php") !== FALSE) {
    $fileName = explode(basename($_SERVER['PHP_SELF']));
    header('Location: '.$fileName[0]);
    die();
}
...
//rest of your page's code
...
?>

Scenario:

  • User goes to: http://www.website.com/page

    • Nothing happens... if block is not executed
  • User goes to: http://www.website.com/page.php

    • if block is executed, redirecting the user to http://www.website.com/page instead.

If you have an include file that you include in all your pages (ex. header.php), then including the if block in there would work as well...

Rushikumar
  • 1,774
  • 5
  • 18
  • 28
  • I need this to happen on ALL the pages of my site. So adding this to every page is problematic... –  May 07 '18 at 20:12
  • @DGall edited the answer... see if that solves it for ya – Rushikumar May 07 '18 at 20:27
  • That didn't work. It also caused problems in my directories and index files (i.e. got page errors for subfolders with index.php files) –  May 07 '18 at 20:35