how to remove php extension from url without htaccess : Change the .html to .php. Remove .php extension from URL. Before proceeding further, you need to enable mod_rewrite in Apache web server.
php remove extension from url
put this piece of code in the root file .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
.htaccess file
RewriteEngine on #remove extension html RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L] #remove extension php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php
rewrite .php to no extension
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]
How to Remove File Extension (.php, .html) from URL using .htaccess?
Removing .php Extension from URL
For example, You required to change URL from http://pakainfo.com/version_1.php to http://pakainfo.com/version_1. Edit .htaccess file and add following settings.
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteEngine on RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC] RewriteRule ^ /%1 [NC,L,R] RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_URI}.php [NC,L]
don’t Miss : remove .php from url htaccess
Removing .html Extension from URL
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^.]+)$ $1.html [NC,L]
Now, If member accessed /version_1 in the browser, it will display the content from /version_1.html. Now, You may required to redirect members which typed complete URL as http://pakainfo.com/version_1.html to the new URL http://pakainfo.com/version_1
RewriteEngine on RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC] RewriteRule ^ /%1 [NC,L,R] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Remove .php extension with .htaccess
To remove file extensions like .php, .html, and .htm using the .htaccess file, you can use the RewriteRule directive. Here’s an example .htaccess code snippet that can be used to remove file extensions:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([^/]+)/?$ $1.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^([^/]+)/?$ $1.html [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.htm -f RewriteRule ^([^/]+)/?$ $1.htm [L]
Here, we have added three RewriteRule directives to remove the .php, .html, and .htm file extensions. The RewriteCond directives are used to check if the requested file exists and is not a directory.
To explain the code, let’s take the first RewriteRule as an example. The rule matches any URL that contains one or more characters, followed by an optional slash, and captures the characters in a group. The $1 in the replacement string refers to the captured group. The rule checks if the requested file with a .php extension exists, and if it does, it rewrites the URL to the same URL without the extension, by appending .php to the captured group.
This way, if you have a file named example.php and a user requests http://example.com/example, the rule will rewrite the URL to http://example.com/example.php.
You can modify this code snippet to fit your specific use case, by changing the file extensions or adding more rules for other extensions.
I hope you get an idea about .
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.