I want to hide .php extension so I wrote following code in .htaccess file which I found at this link How to remove file extension from website address?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This work fine when I type localhost/testsite/index it displays correctly localhost/testsite/index.php and display localhost/testsite/index in address bar as I want,
but when I forcefully type localhost/testsite/index.php it does not converted to localhost/testsite/index. I want to remove extension even when user type .php after page name.
Have your .htaccess like this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Related
i am running Ubuntu 18.04 server and php version is 5.6
https://myurl its index.php auto load when some lick other links
https://myurl/deam its not auto fill with php its should be
https://myurl/dream.php
i did some thing through ht access dont knwo which code block this my ht access code
when i type with .php its work now i want whenever ever link click its auto fill fill with .php how can i do that?
You can use .htaccess with Rewrite
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME}\.php ^(.*)\.php(.*)$
RewriteRule .* %1.php?%2 [QSA]
https://serverfault.com/a/531587
next time try all answers its help to you
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I have a website in which i need to remove every fileĀ“s .php extension, and then redirect any link pointing to any file containing a .php extension to the same file but without the .php.
I have found the following code. It is pretty useful, but anything contained in a folder is gonna be redirected to the root as shown in this example:
http://www.example.com/folder/file.php
redirects to
http://www.example.com/file (without the .php)
The code is this:
RewriteEngine On
#Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#Redirect without .php
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
Any ideas of what could be wrong?
Replace your rules with this one and retest in a new browser or completely clear your browser cache:
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
## To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I know am re requesting the question which are already asked before but i tried all the posts and did all the research but still am unable to over come the problem i tried to put the below second snip first to htaccess in www directory and as well as folder directory the link of the page later i added the small snip it to the htaccess also but still its not working very disappointed my hosting provider is Godaddy if i add .php it works
htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
second
Options -MultiViews
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.elmorfeo\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Following one works for me
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]
I found the answer that was all the above written ware correct but godaddy Cpanel there is no option to show hidden files i accessed it via ftp i found the original the correct htaccess file i did the changes there problem solved
I need to 301 redirect all URLs ending in .php to the non extension version. But the rewrite rules need to work with the existing ones below.
# ==== REWRITE URLS ====
RewriteEngine On
# pass through root
RewriteRule ^(index\.php|Sitemap\.xml)?$ - [L]
# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !\.php$
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(_assets|_css|_fonts|_includes|_scripts)($|/) - [L] #exclude these folders using the 'last: L' flag
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]
This is an extension of this question here.
So the desired result is:
domain.com/region/brand/more-content-here.php
Redirects permanently to:
domain.com/region/brand/more-content-here
But fetches the actual file at:
domain.com/region-brand-more-content-here.php
Tried a few various ideas but they didn't seem to work in with the existing rules and htaccess is not my strength. Also need it to be querystring friendly. Any help would be appreciated.
To remove the .php extension ,you can use :
RewriteEngine on
#1)Permanently redirect "foo.php" to "/foo"#
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
#2)Rewrite "/foo" to "/foo.php"#
#this will internally redirect file to file.php#
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Working code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
I want to redirect from extension URL to extensionless one. For example, /contact should read the contents of /contact.php. I use the following rules to achieve this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
There is a file called test.php that has a variable called "id". I want to redirect read requests from /test/1 to test.php?id=1
I use the following to achieve this:
RewriteRule ^test/([0-9]+)/?$ /test?id=$1
For some reason calling the test/1 file leads to a 500 internal server error. If I remove the rules to hide the php extension, it works again
Also how can I force a permanent redirect on the URL with extension? For example if someone ties to reach /contact.php should be redirected to /contact
Thanks
You can use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L]
This is in my htaccess ... Be sure to have mod_rewrite turned on in Apache...
RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]