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]
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]
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.
How can I use .htaccess to hide .php URL extensions?
Use this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
It should be noted that this will also effect all HTTP requests, including POST, which will subsequently effect all requests of this kind to fall under this redirection and may potentially cause such requests to stop working.
To resolve this, you can add an exception into the first RewriteRule
to ignore POST requests so the rule is not allowed to them.
# To externally redirect /dir/foo.php to /dir/foo excluding POST requests
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
Remove .html and .php extensions with .htaccess
Yes, I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.
Here is the .htaccess code snippet that will help you:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
I want to stress some important things here for everybody's reference:
- This code snippet doesn't remove entry scripts from url (such as
index.php
used by many PHP frameworks) - It only removes
.php
extension, if you want to remove other extension as well (e.g..html
), copy and paste 3rd block and replacephp
with other extension. - Don't forget to also remove extension from anchors (links) href.
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
)
How to create simple remove .php extension with .htaccess page?
My aims is type this url
http://site/Test
it can visithello.php
content, anyone know how to do that?
This isn't simply "file extension removal". If it was then /Test
would serve the content from /Test.php
. In your example, you are wanting to serve the contents of an entirely different file.
Rewriting the request with mod_rewrite
Since you are requesting a directory (/Test/
) and wanting to serve a file from within that directory and the .htaccess
file itself is actually located in that directory, you would need to write your RewriteRule
like this:
RewriteRule ^$ hello.php [L]
In .htaccess
(directory context), the RewriteRule
pattern matches the URL-path relative to the location of the .htaccess
file. So, in this case the URL-path we are matching against is simply an empty string, ie. ^$
, since we are wanting to match /Test/<nothing>
.
Likewise, when the susbtitution string (ie. hello.php
) is relative (as it is here), it is relative to the location of the .htaccess
file. So, in this case it effectively rewrites the request to /Test/hello.php
(it's actually the absolute filesystem path, eg. /var/www/user/public_html/Test/hello.php
- after the directory-prefix is added back).
Additional issues with this example
Since you are rewriting a request that would otherwise map to a physical directory there are a couple of potential issues you need to be aware of...
You should request the directory with a trailing slash, ie. /Test/
, not /Test
(as you stated in your initial example), otherwise mod_dir will issue a 301 redirect to append the slash before your rewrite is successful.
(There are ways to avoid the trailing slash, but this does increase the complexity and requires further manual rewrites.)
An additional complication occurs if there is a DirectoryIndex
document in this subdirectory. eg. /Test/index.php
. In this case mod_dir issues an internal subrequest to the DirectoryIndex
document (eg. index.php
) and this takes priority over your internal rewrite. (Your rewrite does still occur, but mod_dir "wins".) If this is the case then you can rewrite the DirectoryIndex
document instead of an empty URL-path. For example:
RewriteRule ^index\.php$ hello.php [L]
This is perhaps counter-intuitive, as we are now rewriting the internal subrequest that mod_dir has issued in a later pass through the file.
You could handle both scenarios and make index.php
optional. For example:
RewriteRule ^(index\.php)?$ hello.php [L]
RewriteRule ^(.*)$ $1.php
Your example would result in an internal rewrite loop (500 Internal Server Error response) since the pattern ^(.*)$
also matches the rewritten URL and it gets stuck in an endless loop. (The rewriting process doesn't just consist of a single pass through the file. The process repeats until the URL passes through unchanged.)
(Incidentally, this IS an extensionless URL type of rewrite, but it doesn't help you achieve what you stated in your example.)
There are various ways to prevent this "endless loop":
- Use a more specific regex, that won't also match the rewritten URL. eg. a regex that excludes a dot such as
^([^.]+)$
. - Use a
RewriteCond
(condition) directive that prevents the rule being triggered on the rewritten URL. eg. Exclude.php
files or check that the request does not map to a file, etc. - Use the
END
flag on theRewriteRule
to stop all further processing by the rewrite engine.
Alternative - change the DirectoryIndex
Instead of using mod_rewrite, as explained above, to internally rewrite the request, we could instead change the DirectoryIndex
document.
This only works in this particular case where you are requesting a directory and wanting to serve a file from that directory (although strictly speaking the file could be anywhere).
The DirectoryIndex
is the document that mod_dir will look for when requesting a directory (eg. /test/
). By default, it looks for index.html
(and often index.php
) and possibly others. If a DirectoryIndex
document is not found, you get a 403 Forbidden when requesting that directory (assuming auto-directory indexes are disabled).
For example, you could set the following:
DocumentIndex hello.php
And now when you request /Test/
, mod_dir will serve hello.php
in that directory.
However, this method (by itself) is limiting and potentially confusing for readers of your code (if changing the DirectoryIndex
on a directory by directory basis). It is generally expected that the DirectoryIndex
document(s) is consistent throughout your site.
How to hide .php extension in .htaccess
I've used this:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ $1.php [L]
See also: this question
Related Topics
Jquery Mobile: How to Correctly Submit Form Data
Convert Ascii to Utf-8 Encoding
Php-Fpm Doesn't Write to Error Log
Using PHP to Output an Mp4 Video
PHP - Get Base64 Img String Decode and Save as Jpg (Resulting Empty Image )
Include, Include_Once, Require or Require_Once
How to Extract Query Parameters from a Url String in PHP
Get the Latest File Addition in a Directory
How to Call a PHP Script/Function on a HTML Button Click
How to Use Mamp's Version of PHP Instead of the Default on Osx
PHP Case-Insensitive In_Array Function