3

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 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the suggestion, actually yes i agree with you, this becomes less painful when using laravel or php frameworks. but this is an old custom php site, which we don't want to mess up all urls to prevent hurting SEO. So I had to do it through htaccess
So, then you still can built a router XD. Not the best, because of architecture of this site, but anyway. This can help to remove those extensions.
0

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] 

Comments

0

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] 

Comments

0

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] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.