I'm using this in my top level .htaccess to remove the PHP file type extension on my *.php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But I have discovered it also prevents my browser from calling the index.php page in my sub-directories. I need to include /index.php explicitly in the URL.
Is there a better way to remove the PHP file type extension on my *.php files?
here you go:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
or try this one. This should also work in a sub-directory path:
RewriteEngine on
RewriteRule ^(.*)$ $1.php
or in an if clause
<IfModule mod_rewrite.c>
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
Related
I am trying to remove the .php extension only from the file after the first slash.
Right now I have:
example.com/file.php/123123
What I want is:
example.com/file/123123
I have tried adding this code to the .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(e)/?$ /$1.php [L,NC]
It successfully removes the .php extension but only for the last part of the URL.
Meaning it reads:
example.com/file/123123
as:
example.com/file/123123.php
and then makes it unable to load because there is no 123123.php.
How can I make it so it only tries to remove the .php from the after the first slash?
Thanks
If you have mod_rewrite module installed and enabled, you can paste the following code into your apache2.conf file:
<Directory /path/to/site/folder>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
</IfModule>
</Directory>
This module can be installed by default, so you can just try with this code. It may work.
Before I get slapped with a "Duplicate Question," please note that I am inquiring as to how I can add on some mod_rewrite rules to my current .htaccess with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My issue is, I have files outside of the application directory that are not set up with a Controller. So, how can I flat-out remove all .php extensions with my current .htaccess, WHILST retaining the ability to remove the index.php (or, index) parameter from the URL?
EDIT: I have tried another RewriteCond and RewriteRule. CodeIgniter throws a 404, though, since it thinks it is a Controller.
According to Remove .php extension with .htaccess you should add
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
to your .htaccess BUT you should add it before the CodeIgniter rules like so:
Options +FollowSymLinks
RewriteEngine on
# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since mod_rewrite processes and applies rules in order, you want non-CodeIgniter files to be tried first and then fall into the CodeIgniter rules.
I have no affiliation with this site and I am sure there are others like it but if you ever wanted to just test rewrite rules with various URLs then check out https://htaccess.madewithlove.be/
for removing index.php from URL in CodeIgniter. you must follow these steps.
1 . add the following code in .htaccess and .htaccess must be in the root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
2 . change $config['index_page'] = 'index.php'; to $config['index_page'] = '';
you don't need to remove .php in CodeIgniter because that not exist.
if you want to learn! so use this code to remove .php extension
add this code to .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I have my project in my htdocs folder where my main file is index.php. I have tried multiple time to remove the .php extension when the file is opened in the browser, using the .htaccess file with the following lines without luck:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
The extension remains. Is it because the link in my browser is
file:///C:/wamp64/bin/apache/apache2.4.23/htdocs/Cipcpr/index.php
and not
localhost/...
You can use:
Options -MultiViews
RewriteEngine on
# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite with php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
If your project doesn't need to remove extensions dynamically, you can manually set the RewriteRules for every file. Here is the code for your index.php
RewriteEngine On
RewriteRule ^index/?$ index.php [NC,L]
when you now type /index it will reference to /index.php file.
To add other file (let's say login.php) you can just copy last RewriteRule and rewrite the indexes.
RewriteRule ^login/?$ login.php [NC,L]
Just add .htaccess file to the root folder of your site(for example, /home/domains/domain.com/htdocs/) with following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I`m trying to hide my php files extension in my hosted server.i add following code segment in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
but when i try, it is not working. also i tried to do the same thing in my localhost.it is also not working properly.
so do i have to make any other changes to work on it?
The best solution for this is to first:
1. Convert all your .html files to .php
2. Make a .htaccess to hide all known .php extensions
E.g:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
NOTE: If you already have the RewriteEngine on command on top of your .htaccess file, no need of writing it when making new commands.
This method yet is ineffective because when you access localhost/login.php, it would redirect to localhost/login.
So you must do that with php in the individual files!.
Use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
How can I remove .php extension from php files
htacess file
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
any help is much appriciated, Thanks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1.php [NC,L]
If you go to example.com/test/ it will load example.com/test.php
For hat you should define an entry point.
Then you can rewrite your URLs to a parameter of a specified file in that case the index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Then you can filter that parameter in your script and show the correct content.
You have an URL something like:
www.example.de/test-test
Then everything is rewritten to your index.php. Other possibility if you rewrite to an existing file. You can do something like this:
RewriteRule ^(.*)/?$ $1.php [NC,L]
Then you fetch everything behind the slash and call an existing PHP file.
Remove .php extension with .htaccess