隐藏HTML扩展+重定向的.html版本+特殊情况例外重定向、特殊、版本、情况

2023-09-02 10:01:52 作者:藍凋

我有一个很难理解的htaccess的mod_rewrite而我发现了几个相关问题+答案,可惜我不能让我的非常具体的情况,正常工作(mod_rewrite的是,即使在搜索一本书的七印给我的时间说实话)

我在我的根目录foo.html和一个bar.html。现在,我想有foo.html为默认的目录索引(解决,简单),但是从那里我不明白这一点。

我想实现的是:

隐藏的HTML扩展 用户应该能够键入 /条获得 /bar.html 没有看到html的(每一个的.html ) 301重定向的.html版本 用户应该能够键入 /bar.html /条在url(避免重复,为每一位。 HTML) 在最棘手的部分: 由于 foo.html 是默认的目录索引,​​打字/已经具备了(透明) /foo.html ,但我需要键入 /foo.html 来解析/以及打字/富来解析/ 解决方案

尝试把这些规则在htaccess的文件在你的文档根目录:

  RewriteEngine叙述

#1,隐藏的HTML扩展
的RewriteCond%{REQUEST_URI} ^ /(。*?)/?$
的RewriteCond%{DOCUMENT_ROOT} /%1.HTML -f
重写规则^ /%1.html [L]

#2,301重定向的.html版本
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9}  /([^ ] +)。HTML
的RewriteCond%1!为foo $
重写规则^ /%1 [L,R = 301]

#3,打字/foo.html解析为/,以及输入/富解析到/
的RewriteCond%{REQUEST_URI} ^ /(。*?/?)富(。HTML)?$
重写规则^ /%1 [L,R = 301]
 

此外,请确保您有多视图关闭:

 选项-Multiviews
 

第一条规则有2个条件,第一组之间的URI一切首 / 和可能的最后 / 。它使用引用的%1 在未来条件而认为如果 /path/to/www/document/root/%1.html 是存在一个文件。如果两个都是真实的,它在内部重写URI来包括的.html 结尾。

第二条规则有2个的条件下,对​​实际请求所述第一匹配的,而不是到URI(可转变为规则被应用和重写发生)。它认为,如果有与的.html 结尾的请求,如果是的话,第二个条件可以确保它不是 foo.html 请求(自上次规则处理的)。如果两者都属实的话,那将浏览器重定向到URI的一部分,而不HTML,再次使用的%1 引用在比赛分组从第一个条件([ ^ ] +)

最后一个规则检查,如果请求的是一个 foo.html 。如果是这样,重定向除去URI的那部分。

微软Chromium版Edge浏览器支持阻止访问 不安全内容

I have a hard time understanding htaccess mod_rewrite and while I found several related questions + answers, I unfortunately can't get my very specific situation to work correctly (mod_rewrite is even after hours of searching a book of seven seals to me to be honest)

I have foo.html and bar.html within my root directory. Now, I'd like to have foo.html as the default directory index (solved, easy), but from there I do not get it.

What I want to achieve is:

hiding the .html extensions user should be able to type /bar to get /bar.html without seeing the .html (for every .html) 301 redirecting .html version user should be able to type /bar.html and see /bar in the url (avoid duplicate, for every .html) The most tricky part: As foo.html is default directory index, typing / already shows (transparently) /foo.html, but I need typing /foo.html to resolve to / as well as typing /foo to resolve to /

解决方案

Try putting these rules in the htaccess file in your document root:

RewriteEngine

# 1. hiding the .html extensions
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1.html [L]

# 2. 301 redirecting .html version
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^ ]+).html
RewriteCond %1 !foo$
RewriteRule ^ /%1 [L,R=301]

# 3. typing /foo.html to resolve to / as well as typing /foo to resolve to / 
RewriteCond %{REQUEST_URI} ^/(.*?/?)foo(.html)?$
RewriteRule ^ /%1 [L,R=301]

Also, make sure you have Multiviews turned off:

Options -Multiviews

The first rule has 2 conditions, the first groups the URI everything between the first / and a possible last /. The references it using %1 in the next condition which sees if the /path/to/www/document/root/%1.html is a file that exists. If both are true, it internally rewrites the URI to include a .html at the end.

The second rule has 2 conditions, the first matches against the actual request as opposed to the URI (which can change as the rules are being applied and rewrites happen). It sees if there's a request that ends with .html, and if so, the second condition makes sure that it isn't foo.html request (since the last rule handles that). If both are true, then it redirects the browser to the part of the URI without the html, again using %1 to reference the grouping in the match from the first condition ([^ ]+).

The last rule checks if the request is for a foo or foo.html. If so, redirect removing that part of the URI.