使用 .htaccess 删除 .php 扩展名
是的,我已经阅读了 Apache 手册并在这里进行了搜索。由于某种原因,我根本无法让它发挥作用。我最接近的是让它删除扩展名,但它指向根目录。我希望它只在包含 .htaccess
文件的目录中工作。
我需要使用 .htaccess
文件执行三件事。
- 我需要它来删除
.php
a。我有几个使用选项卡的页面,并且 URL 看起来像 page.php#tab< /code> - 这可能吗?
b.我有一个页面使用附加到 URL 的会话 ID 来确保您来自正确的位置,www.domain.example/download-software.php?abcdefg
。
这可能吗?另外,在执行此操作时,我是否需要从标头导航包含文件中的链接中删除 .php
?应该IE“support”是 support?
- 我希望它在每个 URL 之前强制
www
,所以它不是domain.example,但是
www.domain.example/page
。 - 我想从页面中删除所有尾随斜杠。
我会继续寻找、尝试等。位于子目录中会导致任何问题吗?
Yes, I've read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I've come is having it remove the extension, but it points back to the root directory. I want this to just work in the directory that contains the .htaccess
file.
I need to do three things with the .htaccess
file.
- I need it to remove the
.php
a. I have several pages that use tabs and the URL looks like page.php#tab
- is this possible?
b. I have one page that uses a session ID appended to the URL to make sure you came from the right place, www.domain.example/download-software.php?abcdefg
.
Is this possible? Also in doing this, do I need to remove .php
from the links in my header nav include file? Should IE "<a href="support.php">support</a>" be <a href="support">support</a>?
- I would like it to force
www
before every URL, so it's notdomain.example
, butwww.domain.example/page
. - I would like to remove all trailing slashes from pages.
I'll keep looking, trying, etc. Would being in a sub directory cause any issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
Gumbo 在 Stack Overflow 问题中的回答如何使用 Apache mod_rewrite 隐藏 .html 扩展名 应该可以正常工作。
回复 1) 将 .html 更改为 .php
回复 a.) 是的,这是可能的,只需将
#tab
添加到 URL 即可。Re b.) 使用
QSA
(查询字符串附加)可以实现这一点,请参见下文。此应该也可以在子目录路径中使用:
Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.
Re 1) Change the .html to .php
Re a.) Yup, that's possible, just add
#tab
to the URL.Re b.) That's possible using
QSA
(Query String Append), see below.This should also work in a sub-directory path:
Apache mod_rewrite
您正在寻找的是 mod_rewrite,
一般来说,mod_rewrite 的工作原理是将请求的文档与指定的正则表达式进行匹配,然后在内部(在 Apache 进程内)或外部(在客户端浏览器中)执行 URL 重写。这些重写可以像在内部将
example.com/foo
转换为对example.com/foo/bar
的请求一样简单。Apache 文档包含一个
mod_rewrite
指南,我认为其中涵盖了您想要做的一些事情。 详细的 mod_rewrite 指南。强制使用
www
子域重写指南包含以下说明: 规范主机名示例。
删除尾部斜杠(第 1 部分)
我不确定您为什么要这样做,因为重写指南包括 完全相反的示例,即始终包含尾部斜杠。文档表明删除尾部斜杠很可能会导致问题:
也许您可以详细说明为什么要始终删除尾部斜杠?
删除
.php
扩展名我能想到的最接近的做法是在内部重写带有 .php 扩展名的每个请求文档,即
example.com/somepage
被处理为对example.com/somepage.php
的请求。请注意,以这种方式进行将要求每个 somepage 在文件系统上实际上作为 somepage.php 存在。通过正则表达式的正确组合,这在某种程度上应该是可能的。但是,我可以预见一些可能的问题,即未正确请求索引页且未正确匹配目录。
例如,这将正确地将
example.com/test
重写为对example.com/test.php
的请求:但会使
example.com
> 无法加载,因为没有example.com/.php
我猜测,如果您要删除所有尾随斜杠,然后从对目录索引的请求中选择对目录索引的请求父目录中的 filename 将变得几乎不可能。如何确定对目录“foobar”的请求:
从对名为 foobar 的文件(实际上是 foobar.php)的请求
如果您使用
RewriteBase
指令。但如果你这样做,那么这个问题会变得更加复杂,因为你将需要RewriteCond
指令用于执行文件系统级别检查请求是否映射到目录或文件。也就是说,如果您删除删除所有尾部斜杠的要求,而是强制添加尾部斜杠,那么“无 .php 扩展名”问题就会变得更合理一些。
这仍然不完美——每个文件请求仍然有 .php 附加到请求内部。对“hi.txt”的请求会将其放入错误日志中:
但还有另一个选项,设置
DefaultType
和DirectoryIndex
指令,如下所示:Update 2013 -11-14 - 修复了上面的代码片段以纳入 nicorellius 的观察
现在对 hi.txt(以及其他任何内容)的请求都成功了,对
example.com/test
的请求将返回处理后的版本test.php 和index.php 文件将再次工作。我必须给予该解决方案应有的信用,因为我发现了它 Michael J. Radwins 博客,通过在 Google 中搜索php no extension apache。
删除尾部斜杠
一些搜索
apache 删除尾部斜杠
将我带到了一些搜索引擎优化页面。显然,某些内容管理系统(在本例中为 Drupal)会提供 URL 中带或不带尾部斜杠的内容,这在 SEO 领域会导致您的网站遭受重复内容惩罚。 来源该解决方案看起来相当简单,使用
mod_rewrite
我们重写的条件是请求的资源以/
结尾并通过发回301 Permanent Redirect
HTTP 标头来重写 URL。这是他的示例,假设您的域是 blamcast.net,并允许请求可选地添加
www.
前缀。现在我们已经取得进展了。让我们把它们放在一起,看看它是什么样子。
强制
www.
,无.php
,且无尾部斜杠这假设域为
foobar.example
并且在标准端口 80 上运行 。'R' 标志在
RewriteRule
中描述 指令部分。片段:最后注意事项
我无法成功删除斜线。重定向最终给了我无限的重定向循环。在仔细阅读原始解决方案之后,我得到的印象是上面的示例适用于他们,因为他们的 Drupal 安装已配置。他特别提到:
对于以斜线结尾和不以斜线结尾的 URL 而言, 。此外,
Apache mod_rewrite
What you're looking for is mod_rewrite,
Generally speaking,
mod_rewrite
works by matching the requested document against specified regular expressions, then performs URL rewrites internally (within the Apache process) or externally (in the clients browser). These rewrites can be as simple as internally translatingexample.com/foo
into a request forexample.com/foo/bar
.The Apache docs include a
mod_rewrite
guide and I think some of the things you want to do are covered in it. Detailed mod_rewrite guide.Force the
www
subdomainThe rewrite guide includes instructions for this under the Canonical Hostname example.
Remove trailing slashes (Part 1)
I'm not sure why you would want to do this as the rewrite guide includes an example for the exact opposite, i.e., always including a trailing slash. The docs suggest that removing the trailing slash has great potential for causing issues:
Perhaps you could expand on why you want to remove the trailing slash all the time?
Remove
.php
extensionThe closest thing to doing this that I can think of is to internally rewrite every request document with a .php extension, i.e.,
example.com/somepage
is instead processed as a request forexample.com/somepage.php
. Note that proceeding in this manner would would require that each somepage actually exists as somepage.php on the filesystem.With the right combination of regular expressions this should be possible to some extent. However, I can foresee some possible issues with index pages not being requested correctly and not matching directories correctly.
For example, this will correctly rewrite
example.com/test
as a request forexample.com/test.php
:But will make
example.com
fail to load because there is noexample.com/.php
I'm going to guess that if you're removing all trailing slashes, then picking a request for a directory index from a request for a filename in the parent directory will become almost impossible. How do you determine a request for the directory 'foobar':
from a request for a file called foobar (which is actually foobar.php)
It might be possible if you used the
RewriteBase
directive. But if you do that then this problem gets way more complicated as you're going to requireRewriteCond
directives to do filesystem level checking if the request maps to a directory or a file.That said, if you remove your requirement of removing all trailing slashes and instead force-add trailing slashes the "no .php extension" problem becomes a bit more reasonable.
This still isn't perfect -- every request for a file still has .php appended to the request internally. A request for 'hi.txt' will put this in your error logs:
But there is another option, set the
DefaultType
andDirectoryIndex
directives like this:Update 2013-11-14 - Fixed the above snippet to incorporate nicorellius's observation
Now requests for hi.txt (and anything else) are successful, requests to
example.com/test
will return the processed version of test.php, and index.php files will work again.I must give credit where credit is due for this solution as I found it Michael J. Radwins Blog by searching Google for php no extension apache.
Remove trailing slashes
Some searching for
apache remove trailing slashes
brought me to some Search Engine Optimization pages. Apparently some Content Management Systems (Drupal in this case) will make content available with and without a trailing slash in URLs, which in the SEO world will cause your site to incur a duplicate content penalty. SourceThe solution seems fairly trivial, using
mod_rewrite
we rewrite on the condition that the requested resource ends in a/
and rewrite the URL by sending back the301 Permanent Redirect
HTTP header.Here's his example which assumes your domain is blamcast.net and allows the the request to optionally be prefixed with
www.
.Now we're getting somewhere. Lets put it all together and see what it looks like.
Mandatory
www.
, no.php
, and no trailing slashesThis assumes the domain is
foobar.example
and it is running on the standard port 80.The 'R' flag is described in the
RewriteRule
directive section. Snippet:Final Note
I wasn't able to get the slash removal to work successfully. The redirect ended up giving me infinite redirect loops. After reading the original solution closer I get the impression that the example above works for them because of how their Drupal installation is configured. He mentions specifically:
In reference to URLs ending with and without a slash. Furthermore,
除了上面的其他答案之外,
您还可以尝试从文件中完全删除
.php
扩展名并避免无限循环:此代码将在 Root/.htaccess 中工作,
如果要将其放入子目录中的 htaccess 文件,请务必更改 RewriteBase。
在 Apache 2.4 及更高版本上,您还可以使用 END 标志来防止无限循环错误。以下示例在 Apache 2.4 上的工作方式与上述相同,
In addition to other answers above,
You may also try this to remove
.php
extensions completely from your file and to avoid infinite loop:This code will work in Root/.htaccess,
Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.
On Apache 2.4 and later, you can also use the END flag to prevent infinite loop error. The following example works same as the above on Apache 2.4,
以下代码对我来说效果很好:
The following code works fine for me:
我最终得到了以下工作代码:
I've ended up with the following working code:
将
/etc/apache2/apache2.conf
中的参数AllowOverride
从None
更改为All
后 (Debian 8) ,按照这个,.htaccess文件只是必须包含:并且足以从文件中隐藏 .php 扩展名
After changing the parameter
AllowOverride
fromNone
toAll
in/etc/apache2/apache2.conf
(Debian 8), following this, the .htaccess file just must contain:And it was enough to hide .php extension from files
如果您只想对一个特定文件执行此操作,这里有一种方法:
参考:http: //css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
Here's a method if you want to do it for just one specific file:
Ref: http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
试试这个
下面的代码肯定会起作用
Try this
The following code will definitely work
不知道为什么其他答案对我不起作用,但我发现的这段代码确实有效:
这就是我的 htaccess 中的全部内容,并且
example.com/page
显示example.com/page。 php
Not sure why the other answers didn't work for me but this code I found did:
That is all that is in my htaccess and
example.com/page
showsexample.com/page.php
要从 PHP 文件中删除
.php
扩展名,例如将yoursite.example/about.php
删除为yoursite.example/about
: 打开 .htaccess (如果不存在,则创建一个新文件)从网站的根目录创建文件,并添加以下代码。要从 HTML 文件中删除
.html
扩展名,例如将yoursite.example/about.html
删除为yoursite.example/about
: 打开 .htaccess (如果不存在,则创建一个新文件)从网站的根目录创建文件,并添加以下代码。参考:如何从 URL 中删除 PHP 扩展
To remove the
.php
extension from a PHP file for exampleyoursite.example/about.php
toyoursite.example/about
: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.To remove the
.html
extension from a HTML file for exampleyoursite.example/about.html
toyoursite.example/about
: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.Reference: How to Remove PHP Extension from URL
试试这个:-
Try this:-
我发现 100% 适合我的工作概念:
在网站 .htaccess 文件的根目录中使用此代码,例如:
离线 - wamp\www\YourWebDir
在线 - public_html/
I found 100% working Concept for me:
Use this code in Root of your website .htaccess file like :
offline - wamp\www\YourWebDir
online - public_html/
以下是我用来隐藏文件名中的
.php
扩展名的代码:注意:
R=301
用于永久重定向并建议用于 SEO 目的。但是,如果只需要临时重定向,请将其替换为R
Here is the code that I used to hide the
.php
extension from the filename:Note:
R=301
is for permanent redirect and is recommended to use for SEO purpose. However if one wants just a temporary redirect replace it with justR
尝试
Try
如果您使用 PHP 进行编码并希望删除 .php,那么您可以使用如下 URL:
http://yourdomain.example/blah
->它指向 /blah.php这就是您所需要的:
If you're coding in PHP and want to remove .php so you can have a URL like:
http://yourdomain.example/blah
-> which points to /blah.phpThis is all you need:
如果 PHP 中的 URL 类似于
http://yourdomain.example/demo.php
,则类似于http://yourdomain.example/demo
这就是您所需要的:
If your URL in PHP like
http://yourdomain.example/demo.php
than comes likehttp://yourdomain.example/demo
This is all you need:
对于 AWS 托管(Ubuntu 相关服务器)站点
第 1 步:您需要编辑位于以下位置的 apache2 配置文件:
/etc/apache2/apache2.conf
。第 2 步: 您必须编辑如下所示的行:
您需要将
AllowOverride None
替换为AllowOverride All
,然后您需要在终端中启用 apache2 中的重写模式:STEP 3 : 重新启动 Apache
STEP 4 : 然后您可以编辑
.htaccess
文件。For AWS Hosted (Ubuntu related servers) sites
STEP 1 : You need to edit the apache2 config file located in
/etc/apache2/apache2.conf
.STEP 2 : You have to edit the line that look like that:
You need to replace the
AllowOverride None
withAllowOverride All
, then you need to enable rewrite mode in apache2 in the terminal:STEP 3 : Restart Apache
STEP 4 : Then You can Edit
.htaccess
file.