简体   繁体   中英

How to remove .php extension only for pages under a sub directory?

I am trying to remove .php extension for only pages under a subcategory folder of the website.

currently: example.com/blog/my-first-blog.php

what i want: example.com/blog/my-first-blog/

I have tried the following rule in .htaccess (placed in root) but it still shows .php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/blog/$1.php -f
RewriteRule ^(.+?)/?$ /blog/$1.php [L]

The best way to achieve result you want is building a routing. Firstable, you need to rewrite all traffic to your index.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]

Then, you need to write router that will be parsing URL paths. The simplest example of router is:

$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));

if ($url == '/contact/about') {
    include 'contact.php';
}

You can check how does it work in PHP frameworks to get a better solution, or use one of them.

I figured it out I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/ for making sure it only removes .php under "/blog/" folder

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

Try this --

RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.in/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.php $ http://www.example.in/$1 [R=301,L]

Try this

DirectorySlash Off
Options -MultiViews -Indexes
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM