没有.php扩展名的PHP URL格式? [英] PHP URL Format without .php extension?

查看:47
本文介绍了没有.php扩展名的PHP URL格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所需要的只是获取一个字符串作为URL,而不显示PHP文件名.我有文件contact.php页面,我正在尝试获取URL作为 例如

all I need is to get a string as URL without showing the PHP filename. I have file contact.php page I am trying to get URL as e.g.

localhost/contact.php

localhost/contact.php

这就是我现在要访问的页面,只需

this is what now I am getting I need to access this page just with

本地主机/联系人

localhost/contact

对于每个页面,我都只能按名称浏览.

For every page I should able to navigate by their name only.

本地主机/联系人
本地主机/帮助
本地主机/支持

localhost/contact
localhost/help
localhost/support

推荐答案

如果使用的是Apache,则需要使用.htaccess文件.如果您使用的是IIS,则可以在IIS中对其进行配置.

You need to use an .htaccess file if you are using apache. If you are using IIS then you can configure it in IIS.

看看.

您的.htaccess文件需要看起来像这样:

Your .htaccess file would need to look something like this:

IndexIgnore * # prevent directory listing

Order deny,allow
Allow from *

# ------------------------------------------
# Rewrite so that php extentions are not shown
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

由于文件限制,您不能在Windows中简单地创建.htaccess文件,因此请打开文本编辑器(记事本++或您的编程IDE)并创建一个名为.htaccess的新文件. .在文件名前面很重要.

You cannot simply create a .htaccess file in windows because of file constraints, so open a text editor (Notepad ++ or your programming IDE) and create a new file called .htaccess. The . is important in front of the file name.

修改

没有重复的文件名. IE contact.phpcontact.js作为htaccess将不知道要提供哪个,哪个(取决于您的Apache)将返回错误页面,或者仅提供其中一个.

Do not have duplicate file names. IE contact.php and contact.js as the htaccess will not know which one to serve, which (depending on your apache) will either return an error page or it will serve one of them only.

@AedixRhinedale在以下评论中指出:

As @AedixRhinedale pointed out in the comments below:

仅来自 Apache 的注释:如果可以访问httpd主服务器配置文件,则应避免完全使用.htaccess文件.使用.htaccess文件会降低Apache http服务器的速度.可以在.htaccess文件中包含的任何指令最好在Directory块中进行设置,因为它具有相同的效果并具有更好的性能

Just a note from Apache: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance

这篇关于没有.php扩展名的PHP URL格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆