5

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

2 Answers2

4

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]
Croises
  • 18,570
  • 4
  • 30
  • 47
  • It worked expect two things: first: in last RewriteCond i needed to include `/sub` after document root, and second: I now have problem with `/sub/index.php`. it gets redirected to `/sub/index` and gives me 404. I disabled all the lines related to redirection. but it still happens. – Mostafa Ahangarha Oct 08 '18 at 06:41
  • 1
    I change my answer without redirect for `index.php` – Croises Oct 08 '18 at 11:26
  • It still gets redireved to `index`. I have temporarily edited all the links to `index.php` to avoid getting 404. I myself tried several rules to avoid this, but failed. – Mostafa Ahangarha Oct 08 '18 at 11:49
  • It's a problem with your cache. Clear the cache or try with other browser – Croises Oct 08 '18 at 12:27
  • No difference. I tried firefox private mode and also chromium incognito mode. same result. – Mostafa Ahangarha Oct 08 '18 at 13:25
  • Is it possible there be conflict between this htaccess and htaccess in root directory of wordpress? – Mostafa Ahangarha Oct 08 '18 at 13:43
  • Yes, it's possible. Try now with the last correction in my answer code. – Croises Oct 08 '18 at 18:24
  • I myself tried this as well (with $ at end without ^ in beginning) that also didn't work. I tried in private mode as well. Logically it should work. – Mostafa Ahangarha Oct 08 '18 at 19:14
1

You can use Apache mod_rewrite

Just add this code to your root directory .htaccess

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Aaron Yordanyan
  • 738
  • 7
  • 19