Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove .php from URL in subdirectory

Tags:

On a CentOS 6 running apache 2.4 and DirectAdmin, I have a wordpress website. Beside the wordpress standard architecture, I want to have some semi-static pages which are located in a sub-directory.

Filesystem is as: /home/user/public_html/sub/static1.php

Desired URL is as: https://domain/sub/static1

currently I have no problem opening the pages with .php extension. But I want to remove it. I have tried adding some rewrite rule in .htaccess but I have failed since the request gets redirected to homepage.

I have tried to find some solution online, including this website but nothing could help me. I assume DirectAdmin is involved in this issue.

Any help would be appreciated

like image 741
Mostafa Ahangarha Avatar asked Oct 03 '18 12:10

Mostafa Ahangarha


1 Answers

In your /home/user/public_html/sub/.htaccess file, use this code:

Options -MultiViews
RewriteEngine on

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

# rewrite with php php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sub/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
like image 173
Croises Avatar answered Sep 26 '22 19:09

Croises