How to Hide PHP File Extension Without Using .Htaccess

Remove .php extension with .htaccess

Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.

Re 1) Change the .html to .php

Re a.) Yup, that's possible, just add #tab to the URL.

Re b.) That's possible using QSA (Query String Append), see below.

This should also work in a sub-directory path:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

hiding php extension without removing the .php in the actual files

The method you post does exactly what you say you want. You have a "index.php" file on the file system, and your URL is something like "www.mysite.com/index" (no .php extension in the URL).

RewriteEngine on
# The requested filename "/index" is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# There is a file on the file system named as the request, with extension .php
RewriteCond %{REQUEST_FILENAME}.php -f
# If all of the above is true, pretend that the ".php" file had been called
# by rewriting the request appending .php
RewriteRule ^(.*)$ $1.php

You do not need to rename "index.php" as "index".

If you also want to prevent access if .php is specified, place this rule before the one above. Now, "mysite/index" will work, "mysite/index.php" will not.

# Return 404 if original request is .php
RewriteCond %{REQUEST_FILENAME} "\.php$"
RewriteRule .* - [L,R=404]

Please note that there are several other ways in which someone might realize your site uses PHP, and the php-to-404 rule doesn't make much sense.

hide .php extension from address bar also open a page without .php extension

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# 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 %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Error htaccess to hide php extension, just hide html

Assuming your .htaccess file is in the document root of your site then try the following instead:

Options -MultiViews

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.+) $1.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.+) $1.php [L]

The $1 backreference in the RewriteCond TestString (ie. %{DOCUMENT_ROOT}/$1.php) is a backreference to the captured group in the RewriteRule pattern (ie. (.+)), ie. the requested URL-path. This is the same backreference as used in the RewriteRule substitution (ie. $1.php).

A bit more explanation...

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

The condition here that checks the existence of the requested resource plus the file extension is not strictly correct, depending on your file structure. It's not necessarily checking the same file that you will ultimately rewrite to in the following RewriteRule. See my answer to this ServerFault question for a detailed explanation of this.

The L flag is missing on the RewriteRule, so processing will continue through the file and could be getting rewritten again.

MultiViews needs to be disabled (if not already) for the rewrite to be successfully processed. This probably does not affect you currently, but if your rewrite included URL parameters; they would be missing. (The effect of MultiViews is that mod_negotation would issue an internal subrequest for file.php before your mod_rewrite directive is processed.)

htaccess - remove .php extension from url

With your shown samples, please try following htaccess rules file.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^ %1? [R=301,L]

##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]

How can I hide php file extension using htaccess?

@MrWhite gave an answer that showed how to throw an error message like you asked, but why would you want to throw an error message instead of just redirecting example.com/page.php to example.com/page? Here is the .htaccess code for that:

RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

This makes sure that both /page and /page.php go to the same url (/page) and serve the same page/file (/page.php)



Related Topics



Leave a reply



Submit