Tricky Rewrite rule for removing extentions - php

I have the following rule for removing php extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ en/$1.php [NC,L]
Now with these rules it works when I access domain.com/abc .. But the problem is that, when I manually access domain.com/abc.php .. it also works.
What I want is that domain.com/abc.php should also redirect to domain.com/abc.
How can I achieve this ?
In short, in any case I want php extension to be removed.

You can add Rule, which will redirect all *.php to uri-s without extension:
RewriteRule ^(.*).php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ en/$1.php [NC,L]

Related

how to use .htaccess file to remove page detail

This is my .htaaccess code.
#Remove .php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
I need clean url for my website. i have already remove .php extension. how to show just page name like www.xyz.com/courses not like www.xyz.com/page?page-name=Courses?
My Website
I want to show like www.xyz.com/Courses
You can use the following rules :
#Remove .php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#######Clean URLs########
#Redirect /page.php?page-name=Courses to /Courses
RewriteCond %{THE_REQUEST} /page\.php\?page-name=([^\s]+)\s [NC]
RewriteRule .* /%1? [L,R]
#Internally map new URL to the original one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /page.php?page-name=$1 [L]
Make sure to change page.php to your original page name in the condition pattern and in the rewrite destination .

Rewrite rule that will get rid of the .php file extension in my URL

I need a rewrite rule that will get rid of the .php file extension in my URL. I've tried the following. It doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
According to this the following code should work for Removing PHP Extensions.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To remove the .php extension completely , you can use the following rules :
RewriteEngine on
#1 redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^ %1 [L,R]
#2 internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

How to hide php file extension from url from all files?

I have a lot of php files, but I stuck in this that I want to hide php extension from url in for all files without the need to edit my code.
Another problem is that the website use $_GET to caught many parameters from links, headers, forms, is there a way to hide parameters from url or at least to change how it looks without effecting the website functionality.
I tried this but didnot work :
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php\?pid=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php(\ |$)
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)/([0-9]+)$ /$1.php?pid=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try out below code in .htaccess file
# Turn on the rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks.

How to load php file without php extension

I have this working htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]
my question for this if I have that rule on my htaccess how can I load php file that have no .php extension.
I add this to my htacess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It works fine http://localhost/myprojectfolder/account
but this will not work anymore http://localhost/myprojectfolder/1
how can I make them both working ?
Thank you in advance.
Try adding another condition to your php extension rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
that ensures that when you add the .php extension, it actually points to a file that exists.
If you are using this URL http://localhost/myprojectfolder/1 as in your other question. Then test shouldn't even be in the first rule. Then you can just make sure the file exists.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Why it's occurring a error in .htaccess is it because of conditions in it?

i'm working a small project in which i'm using .htaccess to make URLs short and sweet. But it's not working. Here's the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php #removing .php extension
RewriteCond %{SCRIPT_FILENME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 #removing get variables
By using the first condition it's removing the use of .php extension in the URL bar. And the second condition is added with an intention to remove the GET variables. URL is something like this:
www.example.com/category.php?category=latest
and i want this url to be look like this:
www.example.com/category/latest
Flip the order otherwise first rule will catch this category URI also.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

Categories