这是一个后续问题
剩下的问题:一套规则尚未发挥作用
- 目标:标准化
request(\.htm|\.html)
为美丽request
:
RewriteCond %{REQUEST_URI} ".+\.html?$"
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule (.*)/([^/]+)\.html?$ $1/$2 [R=301]
- 正则表达式测试正常:https://regex101.com/r/4rTEVk/3
观察到的行为
- 请求服务
/hello
于底层/hello.html
。- 这是所希望的。在规则中定义
2c) DirectoryIndex features implemented in mod_rewrite
。
- 这是所希望的。在规则中定义
- 请求
/hello.html
按原样提供。- 这是不希望的。
- 因为提供带或不带后缀的同一文件意味着内容重复和不一致性。我想避免这种情况。
- 因为
/hello.htm(l)
我想要强制 HTTP 301 重定向到/hello
.
挑战
- 正如上面的摘录所示,其语法看起来很简单。
- 但与其他规则集的正确相互作用却并非如此。
2a) Serve file as requested
特别是对于在所有其他情况下仍然有效的规则。
完整的.htaccess
## My IA technical concept: All major things work, except one detail.
#
# 1a) Security settings.
# 1b) Explicit requests to index.php CMS router --> Served as-is.
# 2) Static page overlay into CMS namespace
# 2a) /hello — File without file extension exists. --> Served as-is.
# 2b) /hello.html - File with .html extension exists.
# - Serve in canonical form (=without extension) so at: /hello
# - Doesn't work yet. This is where I need help.
# 2c) /hello/index.(htm|html|php) — Folder of that name with index file exists.
# - Note: /hello/ directory listing is explicitly forbidden in Security section (1a)
# - DirectoryIndex rules cannot "fail gracefully", hence recreated as rules in mod_rewrite. Works fine.
# 3) CMS page exists as cached file --> Cached page gets served at beautiful URL.
# 4) /index.php — If nothing of the above matched hand over to CMS index.php (e.g. Wordpress)
## 1a) Security
### Responding as if those files don't exist
RedirectMatch 404 /\.gitignore
### Directory listing OFF
Options -Indexes
### Valid index files
DirectoryIndex index.html index.htm index.php
## 1b) If CMS router is directly requested, serve as-is
RewriteEngine On
RewriteRule ^index\.php$ - [L]
### 2a) Serve file as requested
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
### 2b) Except if it has a .htm or .html suffix
# - For those we will redirect to the canonical URL with the file extension stripped. --> Not yet working!
# - Serve also filenames without extensions.
# Those gets served without a content-type (MIME type). --> Works.
# No problem for those few rare cases as parser of most web browsers detects most important types such as HTML also without a "Content-Type:" header.
# For "/request" check if there's a corresponding "/request.html" and if serve this under "/request"
#### Code block which doesn't work
# - RegEx itself is OK as tested at: https://regex101.com/r/4rTEVk/3
# - Must be a ruleset conflict or order problem
# RewriteCond %{REQUEST_URI} ".+\.html?$"
# RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
# RewriteRule (.*)/([^/]+)\.html?$ $1/$2 [R=301]
#### So in the meanthile I simply serve files with a .htm(l) extension as-is
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.]*[^/])$ $1.html [L]
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^([^.]*[^/])$ $1.htm [L]
## 2c) DirectoryIndex features implemented in mod_rewrite
# - index.(htm|html|php) are valid index files
# - /hello/index.(htm|html|php) will get served at /hello
# If a directory is requested, which is missing the trailing slash then append it
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# Optimisation: If a directory is not requested then skip the next 3 rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . - [S=3]
# NB: Directories end in a trailing slash (enforced above)
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.+)/$ $1/index.html [L]
RewriteCond %{DOCUMENT_ROOT}/$1/index.htm -f
RewriteRule ^(.+)/$ $1/index.htm [L]
RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
RewriteRule ^(.+)/$ $1/index.php [L]
## 3) CMS pages cached to files
# BEGIN W3TC Browser Cache
# ... Various rules which set Cache-Control headers per file type.
# ... Machine generated from Admin UI.
# END W3TC Browser Cache
# BEGIN W3TC Page Cache core
# ... Various redirections rules to cached page representations.
# ... Machine generated from Admin UI.
# END W3TC Page Cache core
## 4) Fallback to CMS
# Note that from the default Wordpress .htaccess two conditions (filesystem checks) are removed.
# - The first one that checks for a "file" is handled by multiple of our rulesets.
# - The second check for a "directory" was removed otherwise directories that do not contain a "DirectoryIndex" are not routed to the CMS.
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress