放置不带文件扩展名的链接 (.php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18018311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:48:37  来源:igfitidea点击:

put links without file extension (.php)

phpapache

提问by Braggae

Is it possible to configure Apache in order not to show a file extension?

是否可以配置 Apache 以不显示文件扩展名?

For example: Say I have domain.com/page.phpbut want to have domain.com/pageas the url.

例如:假设我有domain.com/page.php但想要domain.com/page作为 url。

Any Ideas?

有任何想法吗?

采纳答案by Pascamel

This is called URL rewriting. I had to use it for the first time recently and i used this tutorialto learn it, hope you'll find it great too :

这称为 URL 重写。我最近第一次使用它,我用这个教程来学习它,希望你也会发现它很棒:

回答by JohnnyFaldo

Put this is your .htaccess file

把这是你的 .htaccess 文件

#turn on url rewriting 
RewriteEngine on

#remove the need for .php extention 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ .php

This allows you to access .php files without the extension, so your links should read

这允许您访问没有扩展名的 .php 文件,因此您的链接应为

href="/somepage"

and this will direct to

这将直接指向

href="/somepage.php" 

回答by Seth Battin

There is also MultiViewsas a vhost or .htaccess configuration option. See http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

还有MultiViews一个 vhost 或 .htaccess 配置选项。见http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

From that page:

从该页面:

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

MultiViews的作用如下:如果服务器收到/some/dir/foo的请求,如果/some/dir开启了MultiViews,而/some/dir/foo不存在,则服务器读取目录寻找foo.* 文件,并有效地伪造了一个类型映射,它命名所有这些文件,为它们分配相同的媒体类型和内容编码,如果客户端通过名称要求其中之一。然后它会选择最符合客户要求的方案。

回答by robert_murray

You want a web server feature called URL rewriting - every web server application (apache, IIS, nginx) supports this. As the name suggests, it takes the requested URL and rewrites it into a specific format that you define.

您需要一个称为 URL 重写的 Web 服务器功能 - 每个 Web 服务器应用程序(apache、IIS、nginx)都支持此功能。顾名思义,它接受请求的 URL 并将其重写为您定义的特定格式。

There are many guides available on the www, even if you are using shared hosting solution you can still add/modify the .htaccess file to do this.

www 上有很多指南,即使您使用的是共享主机解决方案,您仍然可以添加/修改 .htaccess 文件来执行此操作。

回答by adzoro ziro

Put this is your .htaccess file

把这是你的 .htaccess 文件

RewriteEngine On
RewriteRule ^page?$ page.php

<a href="page">page</a>