skip to Main Content

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]

4

Answers


  1. Chosen as BEST ANSWER

    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]
    

  2. 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.

    Login or Signup to reply.
  3. 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]
    
    Login or Signup to reply.
  4. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search