This is possible solely using language/content negotiation in the Apache configuration [httpd.conf or apache2.conf] along with rewrite rules. Use of an .htaccess
file is not required.
In apache2.conf file [or httpd.conf] add the Multiviews
directive, MultiviewsMatch
directive, and rewrite rules:
Options Indexes FollowSymLinks Multiviews
AllowOverride None
Require all granted
RewriteEngine On
RewriteCond %{THE_REQUEST} /[[^.]+]\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
MultiviewsMatch Any
Then save the file and restart Apache sudo systemctl restart apache2
If you specify the Options
directive later in httpd.conf, you have to repeat the Multiviews
directive
for each site:
Options FollowSymLinks Multiviews
AllowOverride All
Require all granted
Options FollowSymLinks Multiviews
AllowOverride All
Require all granted
This will not adversely affect content negotiation if you are already serving in multiple languages. For example, replacing example.com/home.php with
home.en.php home.fr.php home.de.php
Then in apache2.conf:
AddLanguage fr .fr
AddLanguage de .de
AddLanguage en .en
LanguagePriority fr de en
ForceLanguagePriority Fallback
Your language will still be negotiated but the php extension will not appear in the URL.
As other people said, just use links like /index.php/nice/looking/url
.
The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess
Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php
in your URL.
Then you can just use a regex match to detect what file to include.
preg_match['@[/]{1}[[a-zA-Z0-9]+]@', $_SERVER["PATH_INFO"], $matches]
[$matches will contain all "parts" of the url in an
array]
Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.
To disallow the use of htaccess, you need this directive in a container for your document root, then you can just place mod_rewrite rules in the same container:
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
assuming that "/var/www/htdocs" is you document root.
To ensure mod_rewrite is loaded, check the httpd.conf file for a LoadModule
line that contains mod_rewrite, and make sure it's uncommented. You'll need to restart your server anytime you make changes to the vhost config.
Short explanation:
The lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
are conditions to check that the request is not an existing file [-f
] or an existing directory [-d
]. These conditions serve 2 main purposes:
- It prevents the rewrite engine from looping, so that
index.php
won't also get rewritten. Sinceindex.php
is an existing file, the conditions stop the rewrite engine. - It allows resources and assets like images or scripts from being rewritten.
If you want
everything routed to index.php
no matter what [including images or anything else], then you can change the 2 conditions to simply:
RewriteCond %{REQUEST_URI} !^/index.php
so that everything gets rewritten except index.php
.
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
- .htaccess file
- rewrite .php to no extension
- How to Remove File Extension [.php, .html] from URL using .htaccess?
- Related posts
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 //pakainfo.com/version_1.php to //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 //pakainfo.com/version_1.html to the new URL //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]
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.