remove .php from file using htaccess - php

I want to tream .php from every url
Like localhost/index.php ---> localhost/index
But I also want localhost/index/ to work too....
Here is the current code I am using
#for hiding.php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)/?$ $1\.php [NC]
############################-------- END ---------########################
Can anybody suggest the changes?

Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [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 .

Remove extensions for both .html and .php in apache

The code is mixed to I have .html and .php in the same folder. There is never a time there there is the same name but different extensions. I have some code that should work but doesn't. The html part works but when I try to request a PHP page without an extension the URL it gives a 404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -F
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -F
RewriteRule ^(.*)$ $1.php
You can try this on the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

Remove .php extesion with .htaccess

I am trying to remove my .php when someone visits a gallery on a client site. I found the following code that enables that but my .jpgs display "image cannot be displayed".
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]`
Any recommendations?
Try to remove .php extensions completely from your file to try to avoid a infinite loop:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
This code will work in Root/.htaccess,
Make sure to change the RewriteBase if you want to place this into a .htaccess file in a sub directory.
On Apache 2.4 and later, you can use the END flag to prevent a infinite loop error. The following example should work the same as the above solution on Apache 2.4.
TRY THIS ALSO:
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Try this, which is easier
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

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 fix .htaccess problems

What I am trying to do it simple:
I want to remove .php extension from file for which I am using following code and it is working
#for hiding.php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
############################-------- END ---------########################
Also I want to show a $_GET parameter on screen for which the link should be link this
localhost/123 which is representation of localhost/somepage.php?para=123
and for which I am using following code which is also working fine
#for parameter Display
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ somepage.php?para=$1
############################-------- END ---------########################
Problem: These codes are working fine when not written together in same `.htaccess` file. But when written together and When I have to access login page using localhost/login. .htaccess treat login as get parameter value. Any solution to this problem?
Try this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ somepage.php?para=$1 [L,QSA]

Categories