Search code examples
apache.htaccessurl-rewritingcentos6directadmin

Remove .php from URL in subdirectory


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


Solution

  • 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]