使用 .htaccess 删除 .php 扩展名

发布于 2024-09-29 00:04:10 字数 840 浏览 2 评论 0原文

是的,我已经阅读了 Apache 手册并在这里进行了搜索。由于某种原因,我根本无法让它发挥作用。我最接近的是让它删除扩展名,但它指向根目录。我希望它只在包含 .htaccess 文件的目录中工作。

我需要使用 .htaccess 文件执行三件事。

  1. 我需要它来删除 .php

a。我有几个使用选项卡的页面,并且 URL 看起来像 page.php#tab< /code> - 这可能吗?

b.我有一个页面使用附加到 URL 的会话 ID 来确保您来自正确的位置,www.domain.example/download-software.php?abcdefg

这可能吗?另外,在执行此操作时,我是否需要从标头导航包含文件中的链接中删除 .php ?应该IE“support”是 support?

  1. 我希望它在每个 URL 之前强制 www,所以它不是 domain.example,但是www.domain.example/page
  2. 我想从页面中删除所有尾随斜杠。

我会继续寻找、尝试等。位于子目录中会导致任何问题吗?

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.

  1. 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>?

  1. I would like it to force www before every URL, so it's not domain.example, but www.domain.example/page.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

木槿暧夏七纪年 2024-10-06 00:04:10

Gumbo 在 Stack Overflow 问题中的回答如何使用 Apache mod_rewrite 隐藏 .html 扩展名 应该可以正常工作。

回复 1) 将 .html 更改为 .php

回复 a.) 是的,这是可能的,只需将 #tab 添加到 URL 即可。

Re b.) 使用QSA(查询字符串附加)可以实现这一点,请参见下文。

应该也可以在子目录路径中使用:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

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:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
海螺姑娘 2024-10-06 00:04:10

Apache mod_rewrite

您正在寻找的是 mod_rewrite

描述:提供基于规则的重写引擎进行重写
动态请求 URL。

一般来说,mod_rewrite 的工作原理是将请求的文档与指定的正则表达式进行匹配,然后在内部(在 Apache 进程内)或外部(在客户端浏览器中)执行 URL 重写。这些重写可以像在内部将 example.com/foo 转换为对 example.com/foo/bar 的请求一样简单。

Apache 文档包含一个 mod_rewrite 指南,我认为其中涵盖了您想要做的一些事情。 详细的 mod_rewrite 指南

强制使用 www 子域

我希望它在每个 URL 之前强制使用“www”,因此它不是 domain.example 而是 www.domain.example/page

重写指南包含以下说明: 规范主机名示例。

删除尾部斜杠(第 1 部分)

我想从页面中删除所有尾部斜杠

我不确定您为什么要这样做,因为重写指南包括 完全相反的示例,即始终包含尾部斜杠。文档表明删除尾部斜杠很可能会导致问题:

尾部斜杠问题

描述:

每个站长都会唱一首关于尾随问题的歌
引用目录的 URL 上的斜杠。如果它们丢失,服务器
转储错误,因为如果您说 /~quux/foo 而不是 /~quux/foo/
然后服务器搜索名为 foo.txt 的文件。并且因为这个文件
是它抱怨的目录。实际上它试图自行修复
大多数情况下,但有时需要模仿这种机制
你。比如当你完成了很多复杂的URL之后
重写 CGI 脚本等。

也许您可以详细说明为什么要始终删除尾部斜杠?

删除 .php 扩展名

我需要它来删除 .php

我能想到的最接近的做法是在内部重写带有 .php 扩展名的每个请求文档,即 example.com/somepage 被处理为对 example.com/somepage.php 的请求。请注意,以这种方式进行将要求每个 somepage 在文件系统上实际上作为 somepage.php 存在。

通过正则表达式的正确组合,这在某种程度上应该是可能的。但是,我可以预见一些可能的问题,即未正确请求索引页且未正确匹配目录。

例如,这将正确地将 example.com/test 重写为对 example.com/test.php 的请求:

RewriteEngine  on
RewriteRule ^(.*)$ $1.php

但会使 example.com > 无法加载,因为没有 example.com/.php

我猜测,如果您要删除所有尾随斜杠,然后从对目录索引的请求中选择对目录索引的请求父目录中的 filename 将变得几乎不可能。如何确定对目录“foobar”的请求:

example.com/foobar

从对名为 foobar 的文件(实际上是 foobar.php)的请求

example.com/foobar

如果您使用 RewriteBase 指令。但如果你这样做,那么这个问题会变得更加复杂,因为你将需要 RewriteCond 指令用于执行文件系统级别检查请求是否映射到目录或文件。

也就是说,如果您删除删除所有尾部斜杠的要求,而是强制添加尾部斜杠,那么“无 .php 扩展名”问题就会变得更合理一些。

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

这仍然不完美——每个文件请求仍然有 .php 附加到请求内部。对“hi.txt”的请求会将其放入错误日志中:

[Tue Oct 26 18:12:52 2010] [error] [client 71.61.190.56] script '/var/www/test.peopleareducks.com/rewrite/hi.txt.php' not found or unable to stat

但还有另一个选项,设置 DefaultTypeDirectoryIndex 指令,如下所示:

DefaultType application/x-httpd-php
DirectoryIndex index.php index.html

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. 前缀。

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

现在我们已经取得进展了。让我们把它们放在一起,看看它是什么样子。

强制 www.,无 .php,且无尾部斜杠

这假设域为 foobar.example 并且在标准端口 80 上运行 。

# Process all files as PHP by default
DefaultType application/x-httpd-php
# Fix sub-directory requests by allowing 'index' as a DirectoryIndex value
DirectoryIndex index index.html

# Force the domain to load with the www subdomain prefix
# If the request doesn't start with www...
RewriteCond %{HTTP_HOST}   !^www\.foobar\.com [NC]
# And the site name isn't empty
RewriteCond %{HTTP_HOST}   !^$
# Finally rewrite the request: end of rules, don't escape the output, and force a 301 redirect
RewriteRule ^/?(.*)         http://www.foobar.example/$1 [L,R,NE]

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?foobar\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

'R' 标志在 RewriteRule 中描述 指令部分。片段:

redirect|R [=code](强制重定向)前缀替换为
http://thishost[:thisport]/ (这使得新的 URL 成为 URI)强制
外部重定向。如果未给出代码,则 HTTP 响应为 302
暂时移动)将被退回。

最后注意事项

我无法成功删除斜线。重定向最终给了我无限的重定向循环。在仔细阅读原始解决方案之后,我得到的印象是上面的示例适用于他们,因为他们的 Drupal 安装已配置。他特别提到:

在正常的 Drupal 站点上,启用干净的 URL,这两个地址
基本上可以互换

对于以斜线结尾和不以斜线结尾的 URL 而言, 。此外,

Drupal 使用名为 .htaccess 的文件来告诉您的 Web 服务器如何
处理 URL。这是启用 Drupal 的干净 URL 的同一个文件
魔法。通过添加一个简单的重定向命令到你的
.htaccess 文件,您可以强制服务器自动删除任何
尾部斜杠。

Apache mod_rewrite

What you're looking for is mod_rewrite,

Description: Provides a rule-based rewriting engine to rewrite
requested URLs on the fly.

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 translating example.com/foo into a request for example.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 subdomain

I would like it to force "www" before every URL, so its not domain.example but www.domain.example/page

The rewrite guide includes instructions for this under the Canonical Hostname example.

Remove trailing slashes (Part 1)

I would like to remove all trailing slashes from pages

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:

Trailing Slash Problem

Description:

Every webmaster can sing a song about the problem of the trailing
slash on URLs referencing directories. If they are missing, the server
dumps an error, because if you say /~quux/foo instead of /~quux/foo/
then the server searches for a file named foo. And because this file
is a directory it complains. Actually it tries to fix it itself in
most of the cases, but sometimes this mechanism need to be emulated by
you. For instance after you have done a lot of complicated URL
rewritings to CGI scripts etc.

Perhaps you could expand on why you want to remove the trailing slash all the time?

Remove .php extension

I need it to remove the .php

The 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 for example.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 for example.com/test.php:

RewriteEngine  on
RewriteRule ^(.*)$ $1.php

But will make example.com fail to load because there is no example.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':

example.com/foobar

from a request for a file called foobar (which is actually foobar.php)

example.com/foobar

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 require RewriteCond 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.

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

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:

[Tue Oct 26 18:12:52 2010] [error] [client 71.61.190.56] script '/var/www/test.peopleareducks.com/rewrite/hi.txt.php' not found or unable to stat

But there is another option, set the DefaultType and DirectoryIndex directives like this:

DefaultType application/x-httpd-php
DirectoryIndex index.php index.html

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. Source

The 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 the 301 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..

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

Now we're getting somewhere. Lets put it all together and see what it looks like.

Mandatory www., no .php, and no trailing slashes

This assumes the domain is foobar.example and it is running on the standard port 80.

# Process all files as PHP by default
DefaultType application/x-httpd-php
# Fix sub-directory requests by allowing 'index' as a DirectoryIndex value
DirectoryIndex index index.html

# Force the domain to load with the www subdomain prefix
# If the request doesn't start with www...
RewriteCond %{HTTP_HOST}   !^www\.foobar\.com [NC]
# And the site name isn't empty
RewriteCond %{HTTP_HOST}   !^$
# Finally rewrite the request: end of rules, don't escape the output, and force a 301 redirect
RewriteRule ^/?(.*)         http://www.foobar.example/$1 [L,R,NE]

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?foobar\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

The 'R' flag is described in the RewriteRule directive section. Snippet:

redirect|R [=code] (force redirect) Prefix Substitution with
http://thishost[:thisport]/ (which makes the new URL a URI) to force
a external redirection. If no code is given, a HTTP response of 302
(MOVED TEMPORARILY) will be returned.

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:

On a normal Drupal site, with clean URLs enabled, these two addresses
are basically interchangeable

In reference to URLs ending with and without a slash. Furthermore,

Drupal uses a file called .htaccess to tell your web server how to
handle URLs. This is the same file that enables Drupal's clean URL
magic. By adding a simple redirect command to the beginning of your
.htaccess file, you can force the server to automatically remove any
trailing slashes.

撩动你心 2024-10-06 00:04:10

除了上面的其他答案之外,

您还可以尝试从文件中完全删除 .php 扩展名并避免无限循环

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

此代码将在 Root/.htaccess 中工作,
如果要将其放入子目录中的 htaccess 文件,请务必更改 RewriteBase。

在 Apache 2.4 及更高版本上,您还可以使用 END 标志来防止无限循环错误。以下示例在 Apache 2.4 上的工作方式与上述相同,

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

In addition to other answers above,

You may also try this to remove .php extensions completely from your file and to avoid infinite loop:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

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,

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
独守阴晴ぅ圆缺 2024-10-06 00:04:10

以下代码对我来说效果很好:

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

The following code works fine for me:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
咽泪装欢 2024-10-06 00:04:10

我最终得到了以下工作代码:

RewriteEngine on 
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

I've ended up with the following working code:

RewriteEngine on 
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
°如果伤别离去 2024-10-06 00:04:10

/etc/apache2/apache2.conf 中的参数 AllowOverrideNone 更改为 All 后 (Debian 8) ,按照这个,.htaccess文件只是必须包含:

Options +MultiViews
AddHandler php5-script php
AddType text/html php

并且足以从文件中隐藏 .php 扩展名

After changing the parameter AllowOverride from None to All in /etc/apache2/apache2.conf (Debian 8), following this, the .htaccess file just must contain:

Options +MultiViews
AddHandler php5-script php
AddType text/html php

And it was enough to hide .php extension from files

意犹 2024-10-06 00:04:10

如果您只想对一个特定文件执行此操作,这里有一种方法:

RewriteRule ^about$ about.php [L]

参考: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:

RewriteRule ^about$ about.php [L]

Ref: http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/

今天小雨转甜 2024-10-06 00:04:10

试试这个
下面的代码肯定会起作用

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

Try this
The following code will definitely work

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
花开半夏魅人心 2024-10-06 00:04:10

不知道为什么其他答案对我不起作用,但我发现的这段代码确实有效:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

这就是我的 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:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

That is all that is in my htaccess and example.com/page shows example.com/page.php

那请放手 2024-10-06 00:04:10

要从 PHP 文件中删除 .php 扩展名,例如将 yoursite.example/about.php 删除为 yoursite.example/about: 打开 .htaccess (如果不存在,则创建一个新文件)从网站的根目录创建文件,并添加以下代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

要从 HTML 文件中删除 .html 扩展名,例如将 yoursite.example/about.html 删除为 yoursite.example/about: 打开 .htaccess (如果不存在,则创建一个新文件)从网站的根目录创建文件,并添加以下代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

参考:如何从 URL 中删除 PHP 扩展

To remove the .php extension from a PHP file for example yoursite.example/about.php to yoursite.example/about: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

To remove the .html extension from a HTML file for example yoursite.example/about.html to yoursite.example/about: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Reference: How to Remove PHP Extension from URL

水中月 2024-10-06 00:04:10

试试这个:-

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

Try this:-

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
空名 2024-10-06 00:04:10

我发现 100% 适合我的工作概念:

# Options is required by Many Hosting
Options +MultiViews

RewriteEngine on

# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

在网站 .htaccess 文件的根目录中使用此代码,例如:

离线 - wamp\www\YourWebDir

在线 - public_html/

如果无法正常工作,请更改 Wamp 的设置
服务器:1) 左键单击 WAMP 图标 2) Apache 3) Apache 模块 4) 左键
点击rewrite_module

I found 100% working Concept for me:

# Options is required by Many Hosting
Options +MultiViews

RewriteEngine on

# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Use this code in Root of your website .htaccess file like :

offline - wamp\www\YourWebDir

online - public_html/

If it doesn't work correct, then change the settings of your Wamp
Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left
click rewrite_module

复古式 2024-10-06 00:04:10

以下是我用来隐藏文件名中的 .php 扩展名的代码:

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

注意:R=301 用于永久重定向并建议用于 SEO 目的。但是,如果只需要临时重定向,请将其替换为 R

Here is the code that I used to hide the .php extension from the filename:

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

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 just R

像极了他 2024-10-06 00:04:10

尝试

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php [L] 

Try

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php [L] 
黑凤梨 2024-10-06 00:04:10

如果您使用 PHP 进行编码并希望删除 .php,那么您可以使用如下 URL:

http://yourdomain.example/blah ->它指向 /blah.php

这就是您所需要的:

<IfModule mod_rewrite.c>
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

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.php

This is all you need:

<IfModule mod_rewrite.c>
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
够钟 2024-10-06 00:04:10

如果 PHP 中的 URL 类似于 http://yourdomain.example/demo.php ,则类似于
http://yourdomain.example/demo

这就是您所需要的:

create file .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If your URL in PHP like http://yourdomain.example/demo.php than comes like
http://yourdomain.example/demo

This is all you need:

create file .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
小镇女孩 2024-10-06 00:04:10

对于 AWS 托管(Ubuntu 相关服务器)站点

第 1 步:您需要编辑位于以下位置的 apache2 配置文件:
/etc/apache2/apache2.conf

sudo nano /etc/apache2/apache2.conf

第 2 步: 您必须编辑如下所示的行:

<Directory /var/www/html/>
  Option Indexes FollowSymbLinks
  AllowOverride None    <-------- FOCUS ON THIS
  Require all granted
</Directory>

您需要将 AllowOverride None 替换为 AllowOverride All,然后您需要在终端中启用 apache2 中的重写模式:

sudo a2enmod rewrite

STEP 3 : 重新启动 Apache

sudo service apache2 restart

STEP 4 : 然后您可以编辑 .htaccess 文件。

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

For AWS Hosted (Ubuntu related servers) sites

STEP 1 : You need to edit the apache2 config file located in
/etc/apache2/apache2.conf.

sudo nano /etc/apache2/apache2.conf

STEP 2 : You have to edit the line that look like that:

<Directory /var/www/html/>
  Option Indexes FollowSymbLinks
  AllowOverride None    <-------- FOCUS ON THIS
  Require all granted
</Directory>

You need to replace the AllowOverride None with AllowOverride All, then you need to enable rewrite mode in apache2 in the terminal:

sudo a2enmod rewrite

STEP 3 : Restart Apache

sudo service apache2 restart

STEP 4 : Then You can Edit .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
友谊不毕业 2024-10-06 00:04:10
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*
quot;   
RewriteRule .* - [L,R=404]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*
quot;   
RewriteRule .* - [L,R=404]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文