Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing .php from url [duplicate]

Tags:

php

.htaccess

Well, I know that it should be done by .htaccess my .htaccess is in the root of my site and here it's content

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Also mod rewrite is enabled at my server. I have a form with an action on ./getReadings.php in it. But when I change it to ./getReadings it says that tehre isn't such file on server. What's my mistake?

like image 329
Sergey Scopin Avatar asked May 20 '26 20:05

Sergey Scopin


2 Answers

Try adding Options -MultiViews before RewriteEngine directive

Options -MultiViews
RewriteEngine On

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

Note: MultiViews is about Apache content negociation (which is the problem here since filename will automatically be translated as filename.php existing file). That's why you have to disable it.


EDIT: right now, you can access same content by 2 different urls (with or without php extension). To avoid duplicate content (which is bad for search engines) you can redirect extensions to extensionless equivalent with this code

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
RewriteRule . /%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
like image 98
Justin Iurman Avatar answered May 22 '26 08:05

Justin Iurman


Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
like image 35
Mad Angle Avatar answered May 22 '26 10:05

Mad Angle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!