Removing .php extensions from files in htdocs - php

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

Related

htaccess rewrite rule remove subdirectory and .php extension

I need to rewrite the url of my web and make it hide subfolder and the .php extension, like this:
original : http://192.168.100.5/project/controller/report.php?unit=1&year=2021
goal : http://192.168.100.5/project/report?unit=1&year=2021
My current rules which doesnt work
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ controller/$1.php [L]
How to achieve this? thankyou
You will need 2 .htaccess files.
site root .htaccess:
RewriteEngine On
# forward everything to controller/ if not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* controller/$0 [L]
controller/.htaccess:
RewriteEngine On
# add .php extension internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Hide file extension out of folders with .htaccess

My website is like organized like this:
index.php
products.php
files
file_manager.php
another_file.php
I want to remove the extensions only from my files at root. So in this case, index.php should be index and products.php should be products but I don't want to touch on the file extensions inside folders. So file_manager.php and another_file.php should stay with the extensions
At this point my code is like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php
but this code removes extensions from folders do.
How can I change this?
Tweak your regex to ensure that it affects root files only:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+([^/]+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+?)/?$ $1.php [L]

Using htaccess to redirect user profiles

I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]

Remove file extensions from my Php files

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

.HTACCESS not to redirect a specific file

I have a web structure where all my files have extension .php and I am using .htaccess to remove .php from the url.
So the file is www.abc.com/ab.php ----> www.abc.com/ab
This works fine, However, I have few files that are requested in form and are manipulated using JS (this isn't ajax) and because of .htaccess it doesnt work.
Can any one let me know how can I exclude "formfile.php" file from not getting redirected to "formfile"
Here is my code :-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
Add this line of code: RewriteRule ^ajax.php.*$ - [PT]

Categories