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]
How can I remove the .php extension from my PHP page?
According to this website:
Change your .htaccess
file like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can do something similar, with the .html
extension, except you change the last line to:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If you don't have a .htaccess
file, do what @Cagy79 said:
You could make a directory
/verification/
and put your script in anindex.php
.In this way a user can go to
www.yoursite/verification/
without the need of adding the index.php part
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 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.
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 to remove .php extension only for pages under a sub directory?
I figured it out
I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/
for making sure it only removes .php under "/blog/" folder
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Related Topics
Algorithm That Will Take Numbers or Words and Find All Possible Combinations
Python Equivalent of PHP's Compact() and Extract()
PHP - Convert Multidimensional Array to 2D Array with Dot Notation Keys
Search for PHP Array Element Containing String
Use a Variable to Define a PHP Function
Smtp Configuration for PHP Mail
Int((0.1+0.7)*10) = 7 in Several Languages. How to Prevent This
Can't Send Email to Addresses at My Own Domain
Storing Form Data as a Session Variable
Create New Variables from Array Keys in PHP
Upload 1Gb Files Using Chunking in PHP
Laravel App Stopped Working After Upgrading to PHP 8
How to Use Multiple Databases Dynamically for One Model in Cakephp