Removing .php extensions from files in htdocs

心不动则不痛 提交于 2019-12-24 08:21:50

问题


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/... 

回答1:


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]



回答2:


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]



回答3:


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


来源:https://stackoverflow.com/questions/40498430/removing-php-extensions-from-files-in-htdocs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!