如何从地址栏中隐藏 .php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5831683/
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 22:37:15  来源:igfitidea点击:

how to hide .php from address bar

phpurladdress-bar

提问by World

I use php to build web applications, but i want my web pages without .php extension in the browser's address bar. For eample http://www.example.com/index.phpshows like http://www.example.com/indexin the browser's address bar.

我使用 php 构建 Web 应用程序,但我希望我的网页在浏览器的地址栏中没有 .php 扩展名。对于eamplehttp://www.example.com/index.php节目,如http://www.example.com/index在浏览器的地址栏中。

How can i do this?

我怎样才能做到这一点?

回答by Karl Laurentius Roos

Put this in a file named .htaccessin your WWW-root:

把它放在一个.htaccess在你的 WWW 根目录中命名的文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ .php

This works if you're running Apache and have mod_rewrite activated.

如果您正在运行 Apache 并激活了 mod_rewrite,这将起作用。

回答by Purushottam

Just create a .htaccess file in wamp/www/and copy-paste this code..

只需在wamp/www/ 中创建一个 .htaccess 文件并复制粘贴此代码..

Options +FollowSymlinks


RewriteEngine On


RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f


RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.+)$ /.php [L,QSA]

Hope! this would be useful for someone!!

希望!这对某人有用!!

回答by Ikhwanul Fikri

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.*?)/?$ .php [L]

回答by Dutchie432

You'll want to find the appropriate method of url-rewritingfor your web server. It lets you map

您需要为您的 Web 服务器找到合适的url 重写方法。它可以让你映射

www.domain.com/page

www.domain.com/page

to

www.domain.com/page.php

www.domain.com/page.php

回答by SeraM

On systems using the Apache webserver, you would use mod_rewrite.

在使用 Apache 网络服务器的系统上,您将使用 mod_rewrite。

On newer versions of IIS, you can use their version of mod_rewrite. On older versions you need to buy a plugin.

在较新版本的 IIS 上,您可以使用他们的 mod_rewrite 版本。在旧版本上,您需要购买插件。

Stack Overflow article

堆栈溢出文章

Search Stack Overflow and you should find answers to questions already asked.

搜索 Stack Overflow,您应该会找到已经提出的问题的答案。

回答by REA_ANDREW

Just to point out that on older versions of IIS for example IIS6 and assuming you are in a 32 bit process then IONICS ISAPI Rewrite is a fantastic free url rewriting module. Inside of 64 bit in IIS 6 I have found the commercial product Helicon ISAPI Rewrite 3 to be a great tool. But if you are in 32 bit, IONICS is free and does everything you will require.

只是要指出,在旧版本的 IIS 上,例如 IIS6,假设您处于 32 位进程中,那么 IONICS ISAPI Rewrite 是一个很棒的免费 url 重写模块。在 IIS 6 的 64 位内部,我发现商业产品 Helicon ISAPI Rewrite 3 是一个很好的工具。但是,如果您使用的是 32 位,则 IONICS 是免费的并且可以满足您的所有需求。

http://iirf.codeplex.com/

http://iirf.codeplex.com/

回答by pavium

In apache2.conf I have

在 apache2.conf 我有

<Files data>    
 ForceType application/x-httpd-php    
</Files>

Which means datais treated as a PHP file without the extension

这意味着data被视为没有扩展名的 PHP 文件

回答by Agrajag

There are several ways of doing it.

有几种方法可以做到。

You can use mod-rewrite to rewire foo to foo.php so that requests for /bar gets handled by /bar.php.

您可以使用 mod-rewrite 将 foo 重新连接到 foo.php,以便 /bar.php 处理对 /bar 的请求。

You can use directories, and default-files, so that you link to the direcory /foo/ which gets handled by /foo/index.php

您可以使用目录和默认文件,以便链接到由 /foo/index.php 处理的目录 /foo/

You can set a php-script as the handler for 404-errors, then you just link to nonexistant files, and the handler-file deals with it however it likes. (typically by using some sort of map from url to php-file)

您可以将 php-script 设置为 404 错误的处理程序,然后您只需链接到不存在的文件,处理程序文件就可以随意处理它。(通常通过使用某种从 url 到 php 文件的映射)

You can tell your webserver that all request for a certain webserver, is to be handled by php.

您可以告诉您的网络服务器,对某个网络服务器的所有请求都将由 php 处理。

The first or second solution is the simplest, but the 2 last ones gives the best flexibility, and variants thereof is what most of the bigger frameworks do.

第一个或第二个解决方案是最简单的,但最后两个解决方案提供了最好的灵活性,大多数大型框架都采用其变体。

回答by mcgrailm

Here is a beginners tutorialfor URL rewriting.

这是一个URL 重写的初学者教程