Removing .php and .html with .htaccess - php

I am attempting to remove .html and .php extensions from my website's URLs. I have been suggested to use the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This indeed works, however it will only work for one extension at a time. So I can either have .html removed, or .php removed from the URL. I need to have both removed simultaneously. Someone suggested I just rename all my .html files to .php, however this will not work for me. Previously, I was able to remove both extensions purely using .htaccess, but am unable to find the solution that I used before.
I also tried this code and it only removes the extension for .html:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What code do I need to make this work?
Thank you in advance for your assistance!

if php files have more prioritet, than html ones, swap groups of lines
RewriteEngine On
# if exist file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# if exist file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

Removing .php makes problems with .inc

I just removed the .php extension from my website by using the code
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But now my login.inc.php makes an error?
How to fix please.
I found out that i just need to make another .htaccess file under the directory of the .inc.php file with the same code and now it works!

how to hide .php .html extension using .htaccess

How do I remove or hide php extension? using .htaccess?
2 Example URL :
http://localhost/folder/test.php?id=1&company=ABC&address=AAAAA
http://localhost/folder/test2.php?username=abcd
Thank You
Add this code in .htaccess for html and php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L] //for php extension
RewriteRule ^([^\.]+)$ $1.html [NC,L] //for html extension
for query string add these lines
Try this
RewriteRule ^(folder)/(\d+)/([^/.]+)$ test.php?id=$1&company=$2&address=$3 [L]
Try This
RewriteRule ^(folder)/(\d+)/([^/.]+)$ test?id=$1&company=$2&address=$3 [L]
Try this
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_0-9]+)$ test?id=$1&company=$2&address=$3 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_0-9]+)$ test2?username=$1 [L]
i have add every possible solution

How to remove .php extensions without changing all links

How I can do to remove .php extension (and html if it's possible in all link) without changing all links ?
I have search and I have found this solution :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
It's work good but there are a problem, in fact in my header (or all post form), my link looks like this :
Action
So then users click on, the url is :
mysite.com/action.php
How I can do to all my url don't have .php exentison without changing all links in my website
Thank you
This will Work
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
On apache 2.4 and later, END flag is used to prevent infinite loop error.

Removing the PHP file type extension

I'm using this in my top level .htaccess to remove the PHP file type extension on my *.php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But I have discovered it also prevents my browser from calling the index.php page in my sub-directories. I need to include /index.php explicitly in the URL.
Is there a better way to remove the PHP file type extension on my *.php files?
here you go:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
or try this one. This should also work in a sub-directory path:
RewriteEngine on
RewriteRule ^(.*)$ $1.php
or in an if clause
<IfModule mod_rewrite.c>
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

how hide extension of my php files

I`m trying to hide my php files extension in my hosted server.i add following code segment in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
but when i try, it is not working. also i tried to do the same thing in my localhost.it is also not working properly.
so do i have to make any other changes to work on it?
The best solution for this is to first:
1. Convert all your .html files to .php
2. Make a .htaccess to hide all known .php extensions
E.g:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
NOTE: If you already have the RewriteEngine on command on top of your .htaccess file, no need of writing it when making new commands.
This method yet is ineffective because when you access localhost/login.php, it would redirect to localhost/login.
So you must do that with php in the individual files!.
Use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Categories