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

can anyone help me to remove .html or .php extension in URL.
I am trying this below code in my php project, but it is not working.
I have tried so many .htaccess codes, but nothing helps me..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.php
Thanks in advance

Previously I also faced problem with removing .html or .php extension form URL.
I have a Solution Here
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
put the above code in .htaccess file, above code is for .php files.
for .html files go through the below code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
I just replaced the .php with .html
Hope it helps You.. :)

Related

PHP url rewrite doesnt happen even if I have configured in htaccess file

I have added .htaccess file inside my PHP project folder with the below content,
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
But when I try to access /project/enviroment if gives me a 404 page. and it doesnt refer the environment.php file internally. Is there any other configurations that I need to do? Thank you.

.htaccess hide page extension

Whenever I attempt to hide the .php extension with .htacess and go to a page using the .php extension all that is displayed on the screen is "Page Not Found". I had it working perfectly before, but I have recently changed hosting companies and now I am using Plesk Onyx 17.0.17 with CentOS Linux 7.3.1611 and PHP 7.1.4.
Here are the contents of my .htaccess file:
Options -Multiviews -Indexes
# Disable Automatic Directory detection
DirectorySlash Off
ErrorDocument 404 /!404.php
RewriteEngine On
RewriteBase /
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/+$ $1 [R=301,L]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have it working now thanks to Anubhava's answer on this question. I still have no idea why my old could would not work though.
Here is the code that I used to solve the problem:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

cannot hide php extension in .htaccess file on godaddy

Hello this is my code of .htaccess file . i have tried so many codes to hide the code but none id working please help me .. this is my full htaccess file.this hide code i found little bit good but this is showing my page content.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
ErrorDocument 404 http://www.mydomainname.com/
Options +FollowSymLinks
#Disable directory browsing
Options -Indexes
RewriteEngine on
#Redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomainname\.com$
RewriteRule (.*) http://www.mydomainname.com/$1 [R=301,L]
RewriteRule pages/(.*) page.php?page=$1
RewriteRule blog/(.*) blog.php?type=$1
RewriteRule post/(.*)/(.*) post.php?pid=$1&name=$2
RewriteRule news-and-events/(.*)/(.*) news.php?nid=$1&name=$2
i found many code but none is working
Use this .htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

url rewrite to short url

Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
If you can help me. It will be lovely. Thanks
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

php htaccess multiple extensions

Ok for example i want: youtube.com/channel.php?c=pinch
to be : youtube.com/channel/pinch
i have tried the code,but i dont know how to add an extension to an extension.. (channel/pinch), it stays as ex. youtube.com/channel?c=Pinch...
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Could somebody fix the code and add to the code so i have the extension? Thanks!

Categories