Apache RewriteRule: bad flag delimiters “FilesMatch”

huangapple go评论55阅读模式
英文:

Apache RewriteRule: bad flag delimiters "FilesMatch"

问题

这是我的.htaccess文件中的内容:

<FilesMatch "\.pdf$">
    RewriteRule ([^/]+)\.pdf $ - [E=FILENAME:$1]
    <If "%{REQUEST_URI} =~ m#^/wp-content/themes/my_theme/pdf/fr/.*#">
        Header add Link '<https://www.example.com/wp-content/my_theme/theme_SES/pdf/fr/%{FILENAME}e>; rel="canonical"'
    </If>
</FilesMatch>

error.log 中显示以下错误信息:

RewriteRule: bad flag delimiters

.htaccess 文件有什么问题?

英文:

This is my .htaccess file in the head

<FilesMatch "\.pdf$">
    RewriteRule ([^/]+)\.pdf $ - [E=FILENAME:$1]
    <If "%{REQUEST_URI} =~ m#^/wp-content/themes/my_theme/pdf/fr/.*#">
        Header add Link '<https://www.example.com/wp-content/my_theme/theme_SES/pdf/fr/%{FILENAME}e>; rel="canonical"'
    </If>
</FilesMatch>

And I'm receiving the following error in the error.log:

RewriteRule: bad flag delimiters

What's wrong with the .htaccess file?

答案1

得分: 1

> RewriteRule ([^/]+).pdf $ - [E=FILENAME:$1]
应该像这样写:

RewriteRule ([^/]+)\.pdf$ - [E=FILENAME:$1]

Aside: 您在这种情况下应该没有问题,但请注意来自文档的以下警告:

> 虽然重写规则在<Location><Files>部分(包括它们的正则表达式对应部分)中在语法上是允许的,但这是从未必要的,也是不受支持的。在这些上下文中可能会中断的一个常见功能是相对替换。

例如,您的代码块可以更简单地重写为:

RewriteRule ^wp-content/themes/my_theme/pdf/fr/(?:.+/)?([^/]+)\.pdf$ - [E=FILENAME:$1]
Header add Link '<https://www.example.com/wp-content/my_theme/theme_SES/pdf/fr/%{FILENAME}e>; rel="canonical"' env=FILENAME

这可能还可以根据您的实际URL结构进一步简化。

根据您的其他指令,您可能需要将Header指令中的FILENAME更改为REDIRECT_FILENAME。(这取决于重写引擎是否存在循环,如果使用标准的WordPress指令,则通常会有循环。)

请注意,在Header指令上添加了env=FILENAME参数,它根据FILENAME环境变量的设置条件性地设置此标头。

英文:

> RewriteRule ([^/]+).pdf $ - [E=FILENAME:$1]
> ------------------------^

You have an erroneous space before the $. So the - is being seen as the flags (3rd) argument, hence the error.

It should be written like this:

RewriteRule ([^/]+)\.pdf$ - [E=FILENAME:$1]

Aside: You should be OK in this instance, but note the following warning from the docs:

> Although rewrite rules are syntactically permitted in <Location> and <Files> sections (including their regular expression counterparts), this should never be necessary and is unsupported. A likely feature to break in these contexts is relative substitutions.

For example, your code block could be rewritten more simply as:

RewriteRule ^wp-content/themes/my_theme/pdf/fr/(?:.+/)?([^/]+)\.pdf$ - [E=FILENAME:$1]
Header add Link '<https://www.example.com/wp-content/my_theme/theme_SES/pdf/fr/%{FILENAME}e>; rel="canonical"' env=FILENAME

This could perhaps be simplified further knowing your actual URL structure.

And, depending on your other directives, you may need to change FILENAME in the Header directive to REDIRECT_FILENAME. (Depending on whether there is a loop by the rewrite engine - which there usually is if the standard WordPress directives are being used.)

Note the addition of the env=FILENAME argument on the Header directive, which sets this header conditionally on whether the FILENAME env var is set.

huangapple
  • 本文由 发表于 2023年3月20日 23:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定