how to remove .php file extension on static files htaccess - php

I'm trying to remove the file extension from the url of the static pages
sitename.com/page.php → sitename.com/page
I made it so that my wp url looks like this (if it matters)
sitename.com/wp/ → sitename.com/
FTP looks like this:
- root directory
- static php files
- htaccess file
- wp folder
Adding this to .htaccess doesn't do anything
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Current .htaccess contents:
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
ErrorDocument 403 /403.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How do I achieve this by only adding a code in the .htaccess file?
Thank you in advance!

I realized the files are getting overwritten so the only way I can go about it is to create individual folders for each static file (i.e., page/index.php)
so that I can achieve this sitename.com/page.php → sitename.com/page

The steps to remove file extensions are:
Login to your cPanel account.
Go to File Manager – in the FILES Section
In the File Manager go to the Settings button on the top right corner.
On the Preferences window that will appear, check the Show Hidden Files (dotfiles) option. Click the Save button to apply the settings.
Now navigate to the .htaccess file. If the file doesn’t exist you will need to create it.
Click the Edit button from the File Manager top menu.
Add the below lines to the .htaccess file. Click the Save Changes button and then the Close button.
#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Now the Apache web server will remove .php extension from URLs.
To remove .html extension use:
#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
You can also edit .htaccess files on your web hosting account via an
FTP client like FileZilla.

Related

htaccess - add custom rewrite to standard wordpress rewrites

i have a wordpress website
i have uploaded some of my php code at the root of my web where wordpress is installed.
i want to open those page directly.
they open if # www.example.com/newcode.php
but i want to open them like www.example.com/newcode (without extension .php)
the problem is when i put some .htacess code to achieve this, all wordpress pages stops and shows error404 not found and if i remove htaccess code then i cant open my own newcode.php page without extension
i tried to put this code in htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^\.]+)$ $1.php [NC,L]
but doesnt work...
this is my wordpress .htaccess (automatically created by wordpress)
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^\.]+)$ $1.php [NC,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
id have more files like newcode.php and some folder in which there are more code files and i want all of them to open like this
i want both wordpress pages as well as my new code page to open without any extension and i cant put newcode.php in seperate folder and make another htaccess for that specific folder
This is documented pretty clearly - especially in this case where you don't even need any regexp knowledge. Simply replace everything before # BEGIN WordPress with:
RewriteEngine On
RewriteRule /newcode /newcode.php [L]

How to add .php extension for particular file?

In my wordpress website i used a htaccess code to remove php extension. Then I again used a code to add php extension.
After that all files are accessible but when I click on plugins and contactform7 from my dashboard it opens https://ecxsolution.roundabouttech.com/wp-admin/plugins without extension and string after it and shows file not found.
I am not able to see all my forms even. Can somebody tell me how should I change .htaccess file to add extension for this particular file?
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

Remove file extensions with .htaccess

I'm trying to make a page on my site with the URL of http://localhost/logout/.
I've created the /logout/ directory and the index.php file in that folder, but because I remove the extension of files when I go to http://localhost/logout/ it gives me a 404 error.
To actually get to the page I have to go to http://localhost/logout/index, which I don't want.
How do I fix this?
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -Indexes
do not remove the .php extension, add this to htaccess :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Why do I need to add index.php in order to route my website correctly? [getkirby]

I uploaded the code to the server. It started the homepage correctly . But when I press any link , I get 404 not found error. I discovered that I need to add index.php to my url for it to work.
so it will be like that:
mydomain.somee.com/myWebsite/index.php/anotherPage
When I was working locally using Xamp as a server, I didn't get any of those problems.
I got those problems after I uploaded the website to some.com which apparently doesn't use .htaccess file (editing or removing has no effect).
How to add this index.php automatically and hide it from the user?
I didn't change any of the system files or the htaccess
please tell me if you need anymore files or description.
You need to redirect all your all pages through index.php file but remove it from URL.
Write below rules in your root .htaccess file:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
To understand, How htaccess rules are working, This link will help you :)
Hope it will help you :)

htaccess: direct to php if php is present, if not, direct to directory

I have a php file at www.example.com/media.php
I also have a directory of the same name at www.example.com/media/manyotherfiles.php
If I enter my url as www.example.com/media or www.example.com/media/, I want to be directed to the file at www.example.com/media.php, instead of the files inside the work directory.
However, if I enter my url as www.example.com/media/manyotherfiles, I want to be directed to www.example.com/media/manyotherfiles.php. (it's more intuitive as the media.php file links to the pages in the media directory)
How can I do this with my .htaccess file? So far I have this to remove the .php file extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ $1.php [L]
Options -Indexes
Also, I am currently working on a beta directory on a local server, so for example, my files would be as such:
ip.address.here/beta/index.php
ip.address.here/beta/media.php
ip.address.here/beta/media/manyotherfiles.php
You can use this code in your DOCUMENT_ROOT/.htaccess file to hide .php extension:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Categories