htaccess 永久重定向抛出内部服务器错误

htaccess permanent redirect throws Internal Server Error

在 Apache 2.2 的 mod_rewrite documentation 中,RewriteRule [R] 标志声明:

Any valid HTTP response status code may be specified, using the syntax [R=305], with a 302 status code being used by default if none is specified. The status code specified need not necessarily be a redirect (3xx) status code. However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.

In addition to response status codes, you may also specify redirect status using their symbolic names: temp (default), permanent, or seeother.

但是下面的代码片段似乎只允许“符号名称”:

# Force HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off

# this works
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=permanent,NE]

# this doesn't work
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=308,NE]

错误日志显示:

[Tue May 23 23:11:12 2017] [alert] [client 192.168.33.1] /var/www/html/ventus/.htaccess: RewriteRule: invalid HTTP response code '308' for flag 'R'

但是,308 似乎是永久重定向的 HTTP 状态代码:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308

这是 Apache 中的错误还是我做错了什么?

您的 apache (2.2) 版本不能使用 308。在 apache 2.4.3 中添加了对 308 状态的支持:https://archive.apache.org/dist/httpd/CHANGES_2.4.3

core: Add missing HTTP status codes registered with IANA.

是的,这是一个错误。您对文档的解释是正确的,应该接受 300-399 范围内的所有代码。