0

I'm trying to accomplish 2 things in htaccess that is located in my root directory and should affect all web pages throughout my website. I'm using Apache 2.4.

1) Rewrite non-www to www (this part is currently working)

2) Remove .php extension from URL

For #2 I found two threads about this but when trying to implement it with code from #1 it never seems to work.

Remove .php extension with .htaccess

How to hide the .html extension with Apache mod_rewrite

Here is my code, the first part works fine but the second part doesn't seem to do anything for me as ".php" still shows up on all URL's.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

I also looked at this thread and tried the following, still not working.

5 .htaccess Rewrites: Force HTTPS, Remove index.php, Remove .php, Force www, Force Trailing Slash

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Any help would be appreciated. Thanks in advance.

UPDATED

Here is the folder structure and where the PHP code exists. Most php files are in the root folder but there are two additional folders that contain PHP code.

/
index.php
file1.php
file2.php
file3.php
file4.php
folder1/file5.php
folder1/file6.php
folder2/file7.php
folder2/file8.php
Community
  • 1
  • 1
Matt
  • 2,500
  • 1
  • 16
  • 26
  • Where's the php code? –  Nov 19 '16 at 18:24
  • @Hallur I've updated the post to include php file locations. – Matt Nov 19 '16 at 18:43
  • the question was rhetorical... You have added a "php" tag in your post, but your question has nothing to do with php... It is only about htaccess. Point being, you shouldn't use tags that are irrelevant. –  Nov 19 '16 at 18:46

1 Answers1

1

You can use:

RewriteEngine On
Options -MultiViews

# Redirect to http(s)://www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Rewrite to php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+)/?$ /$1.php [L]
Croises
  • 18,570
  • 4
  • 30
  • 47
  • I received a "Not Found. The requested URL /myFileName was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request." So now the server changes the URL correctly (thanks by the way) but because there is no .php at the end the server is saying the file can't be found. Any advice? – Matt Nov 20 '16 at 04:06
  • well your code got rid of .php, however my $_POST code when passing variables between pages broke. So now anything that was passing variables doesn't get passed. Not sure what's going on. I'll leave this as the correct answer since technically it does remove .php but I can't use it because it breaks my site. – Matt Nov 22 '16 at 03:10
  • 1
    Add `RewriteCond %{REQUEST_METHOD} !POST` before the line `RewriteCond %{THE_REQUEST}...` – Croises Nov 22 '16 at 07:19
  • 1
    Or remove `.php` in your html code `
    `
    – Croises Nov 22 '16 at 07:22