使用htaccess文件删除html扩展名 [英] remove html extension using htaccess file

查看:96
本文介绍了使用htaccess文件删除html扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常常见的话题,但是没有一种解决方案适合我。
i尝试了几种答案:



答案1



答案2



答案3



和引荐链接。



我正在使用的htaccess代码:

  RewriteEngine On 
RewriteCond%{REQUEST_FILENAME}!-f

RewriteCond%{REQUEST_FILENAME}!-d

RewriteRule ^(。*)\.html $ / $ 1 [L,R = 301]

然后我将链接从services.html更改为services,即删除了扩展名,并且



我得到的是浏览器中显示的页面代码不是实际页面。



我已经检查了服务器上的mod_rewrite,并且使用此代码可以正常工作

  
上的RewriteEngine选项+ FollowSymLinks
RewriteRule ^ joomla\.html http://www.joomla.org/? [R = 301,L]


解决方案

您的文件仍应在服务器上具有 .html 扩展名,否则服务器/浏览器不会将其解释为html而是文本(这是您在浏览器中看到代码的原因) 。因此,再次使用ftp重命名服务器上的文件。



为了让您的服务器提供文件 /test.html 访问URL / test 时,您想在内部进行重写,即:

  RewriteRule ^ test [^ /] $ test.html 

^ 匹配字符串的开头, $ 匹配结尾,因此仅当url完全匹配<时,才重写code> test 。它将将该请求重写为 test.html 。重写是内部的,这意味着它不会重定向浏览器(地址栏中的网址不变)。还有一个外部重写(在规则后附加 [R] ),重定向浏览器。



此规则还只匹配不以斜杠结尾的网址( [^ /] 表示非斜杠)。



您可以为每个想要的页面创建一个自定义的 RewriteRule 重写:

  RewriteRule ^ foo [^ /] $ foo.html 
RewriteRule ^ bar [^ /] $ bar.html

如果您有很多页面,这是很多工作,因此您可能需要重写所有网址,例如 foo bar 应该重写为 foo.html / bar.html foo / bar 应该重写为 foo / bar.html



您还可以使用正则表达式来匹配所有请求。但是,您应该首先检查 foo 是否实际上不是目录(该目录可能包含 index.html 并为您要投放的子目录)。另外,您还要检查 foo 是否真的不是服务器文件系统中的文件。有两个重写条件需要检查(请参见 RewriteCond 指令):

  RewriteCond%{REQUEST_FILENAME}!-d 
RewriteCond%{REQUEST_FILENAME}!-f

现在您可以为所有请求添加重写规则:

  RewriteRule ^(。+ [^ /])$ $ 1.html 

$ 1 是第一个捕获组的内容,正则表达式中的捕获组是放置在()括号。 匹配任何字符, + 修饰符表示一个或多个。



请注意,这将导致重写循环,从而导致500错误(查看此答案),因此您还需要添加一个重写条件,以检查< request> .html 文件在文件系统上是否确实存在:

  RewriteCond%{REQUEST_FILENAME} .html -f 

此外,您可能不想重写扩展名为 .html 的网址。我认为这不是必需的,因为您已经有了上面的规则,在这种情况下,将检查可能不存在的< file> .html.html 。但是,如果需要处理,则可以添加另一个条件:

  RewriteCond%{REQUEST_FILENAME}!^。+ \ .html $ 

因此,将其重写规则总和如下:

  RewriteCond%{REQUEST_FILENAME}!-d 
RewriteCond%{REQUEST_FILENAME}!-f
#RewriteCond%{REQUEST_FILENAME}!^。 + \.html $#不是很必要
RewriteCond%{REQUEST_FILENAME} .html -f
RewriteRule ^(。+ [^ /])$ $ 1.html

您现在唯一需要处理的是带有斜杠的网址。为此,我们只添加一个简单的外部重写规则,如果url与目录实际不匹配,则删除该斜杠:

  RewriteCond%{REQUEST_FILENAME}!-d 
RewriteRule ^(。+)/ $ $ 1 [R,L]

此规则仅匹配以 / (正则表达式 / $ )结尾的网址,并捕获所有将斜杠作为一个组尾随(正则表达式(。+)),然后重定向到该组(不包含斜杠)。请注意 R L 标志 R 用于重定向,它重定向浏览器(地址栏中的URL更改)。 L 是最后一个,这意味着此规则之后将不再应用其他规则,尽管规则将在重写后再次应用,这就是应用另一规则的地方。 / p>




TL; DR

  RewriteBase / 

#处理尾部斜杠(如果不是目录)
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。+ )/ $ $ 1 [R,L]

#在内部添加.html扩展名
RewriteCond%{REQUEST_FILENAME}的重写规则!-d
RewriteCond%{REQUEST_FILENAME}!- f
RewriteCond%{REQUEST_FILENAME} .html -f
RewriteRule ^(。+ [^ /])$ $ 1.html


i know this is a very common topic but none of the solutions works for me. i have tried several answers:

answer 1

answer 2

answer 3

and the referal links.

The htaccess code that i am using:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

then i changed my links from services.html to services i.e. i removed the extension and also removed the extension from the file name using ftp.

what i get is the page code displayed in my browser not the actual page.

I have checked mod_rewrite on my server and it works correctly using this code

RewriteEngine On
Options +FollowSymLinks
RewriteRule ^joomla\.html http://www.joomla.org/? [R=301,L]

解决方案

Your file should still have the .html extension on the server, otherwise the server/browser won't interpret it as html but text instead (which is the reason you see the code in your browser). So rename the files on your server using ftp again.

In order to let your server serve the file /test.html when the url /test is accessed, you want to rewrite that internally, i.e.:

RewriteRule ^test[^/]$ test.html

The ^ is to match the start of the string, the $ matches the end, so this will only rewrite if the url exactly matches test. It will rewrite that request to test.html. The rewrite is internal, this means it won't redirect the browser (the url in the address bar doesn't change). There is also the external rewrite ([R] appended to the rule), which redirects the browser.

This rule also only matches urls that don't end with a trailing slash ([^/] means "not slash"). We will handle urls with a trailing slash later.

You could create a custom RewriteRule for each page that you want to rewrite:

RewriteRule ^foo[^/]$ foo.html
RewriteRule ^bar[^/]$ bar.html

This is a lot of work if you have many pages, so you may want to rewrite all urls, e.g. foo or bar should be rewritten to foo.html / bar.html, and foo/bar should be rewritten to foo/bar.html.

You can also use a regular expression to match all requests. But you should first check if foo isn't actually a directory (which could contain a index.html and be a sub-directory you want to serve). Also you want to check if foo isn't really a file on your server's file system. There are two rewrite conditions to check that (see the RewriteCond directive):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Now you can add a rewrite rule for all requests:

RewriteRule ^(.+[^/])$ $1.html

$1 is the content of the first capture group, a capture group in a regex is an expression placed in () brackets. . matches any character, the + modifier means "one or more".

Note that this would cause a rewrite loop resulting in a 500 error (see this answer), so you need to also add a rewrite condition to check if the <request>.html file actually exists on the file system:

RewriteCond %{REQUEST_FILENAME}.html -f

Also you might not want to rewrite urls that already have a .html extension. I don't think it's necessary, as you already have the rule above which would in that case check for <file>.html.html which probably shouldn't exist. But if you have to deal with that, you could add another condition:

RewriteCond %{REQUEST_FILENAME} !^.+\.html$

So putting it altogether your rewrite rule looks like this:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !^.+\.html$ # not really necessary
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html

The only thing that you need to handle now are urls that have a trailing slash. For that we just add a simple external rewrite rule that removes the trailing slash if the url doesn't actually match a directory:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]

This rule matches only urls that end with / (regex /$) and captures everything before the trailing slash as a group (regex (.+)), then redirects to the group (which doesn't contain the slash). Notice the R and L flags behind the rule. R is for redirect, which redirects the browser (url in address bar changes). L is for last, meaning no other rules will be applied after this one, though rules will be applied again after the rewrite, and this is where the other rule gets applied.


TL;DR

RewriteBase /

# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]

# rewrite rule that internally adds the .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html

这篇关于使用htaccess文件删除html扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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