Remove .php extension in .htaccess with already working friendly-url RewriteRule

Kamikaza

I tried hundreds of answers from this site (questions from other users) but these answers did not worked in my case.

My website is using slugs to display seo-friendly urls and that part is working fine. But I would also like to have some NON dynamic links /static links like this:

example.com/contact.php to be displayed/linked without the .php extension, for example example.com/contact

But nothings seems to work, and after 12 hours this .htaccess is driving me crazy.

Here is what I have done until now

RewriteEngine On

#this is the part for see-friendly urls, this part is working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-/.a-zA-Z0-9]+)$ en.php?slug=$1 [QSA,L] 

#this is the part to remove php extension, this part is NOT working
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Any idea?

EDIT I am linking the users like this example.com/contact but I get error 404

IMSoP

The important thing to remember is that a rewrite rule doesn't "display a pretty URL", it reads the URL sent by the browser, and decides what to do with it. Thinking about "removing the .php from the URL" will put you in the wrong frame of mind.

What you need to be asking is, if the browser sends a URL of "/contact", how should the server decide what page to show - is it "/en.php?slug=contact", or is it "/contact.php"?

The other thing to remember is that order of rules in the file matters - if the URL coming in matches the first rule, the server isn't going to keep looking for a "better" match; particularly since your first rule has the flag "L" for "last".

So the solution in this case is simply to change the order of rules in the file: first put the section to check if there's a PHP file called "contact.php", and then only if that rule didn't match the server will reach the section that says to use "contact" as a slug.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Htaccess redirect seems not working with php file extension to seo friendly (no extension) url

.htaccess RewriteRule .php extension conflict

.htaccess rule to friendly URL not working

Htaccess rewrite to remove .php extension not working with directories

$_GET not working with .htaccess rewrite remove php extension

htaccess - remove .php extension from url

htaccess RewriteRule works but browser does not display friendly URL

Remove php extension and parameters using Mod Rewrite in .htaccess to create two seo friendly urls

Remove php extension in URL if URL does not contain keyword with htaccess

remove file extension htaccess not working

Friendly URL using .htaccess and PHP "include"

Hiding php extension and hide subdirectory both in one url not working in .htaccess

removing .html or .PHP URL extension using .htaccess not working

Remove .php extension is not working

.htaccess redirect a url to PHP using RewriteRule

.htaccess RewriteRule with file extension

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

url extension remove with php

Simple .htaccess RewriteRule not working

simple htaccess RewriteRule is not working

In my htaccess RewriteRule is not working

RewriteRule for .htaccess file not working

How to redirect to clean URL (remove last hypen) with RewriteRule in .htaccess

.htaccess RewriteRule with slash in url

How to remove .html extension from url when .htaccess changes aren't working

Remove php extension from URL - except one and only one URL using htaccess

Remove path & extension on URL via htaccess

removing .html URL extension using .htaccess not working

Not able to make seo friendly URL from php using htaccess