默认为Web服务器上的PHP扩展 [英] Default to PHP extension on web server

查看:122
本文介绍了默认为Web服务器上的PHP扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法.htaccess文件可写,这样

Is there a way a .htaccess file can be written so that

http://www.mysite.com/about 

负荷 about.php

和同去的任何文件?

我试过

RewriteEngine On
RewriteRule ^([0-9A-Za-z]+)$ $1.php

但它似乎并没有工作。

but it doesn't seem to work.

推荐答案

这是最常见的规则(使用的mod_rewrite - 确保它被加载并启用的) - 这将确保这样的PHP文件重写之前确实存在(是的,这是一个有点慢,但更加安全):

This is the most common rule (utilising mod_rewrite -- make sure it is loaded and enabled) -- it will ensure that such .php file does exist before rewriting (yes, it's a bit slower but safer):

Options +FollowSymLinks
RewriteEngine On

# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]


或者只是用这样的:


Alternatively just use this:

Options +MultiViews

这将打开内容协商。但它也有一些缺点。

This will turn on "content negotiation". But it has some cons.

例如:让我们假设你有 hello.html的&放大器; hello.php 在您的网站的根文件夹中。如果您要求 example.com/hello ,与该选项启用Apache会寻找替代姓名(名称相同,但不同的扩展名),将成为无论是你好html的 hello.php (我不知道哪一个会是preferred)。

For example: let's assume you have hello.html & hello.php in your website root folder. If you request example.com/hello, with that option enabled Apache will look for alternative names (same name but different extensions) and will serve either hello.html or hello.php (I cannot tell which one will be preferred).

但如果你只有1文件,例如唯一的名称(例如: hello.php 只),然后在这里没有任何问题。

But if you only have 1 file with such unique name (e.g. hello.php ONLY) then no problems here at all.

这篇关于默认为Web服务器上的PHP扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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