我想要的是:

到 301 重定向 /file_new.pdf 到 /file new.pdf ( %20 又名间距 file 和 new )

我知道:

  • 我可以依赖使用 mod_rewrite 的 RewriteRule 301,使用此线程中的解决方案:@ mod_rewrite url 中带有空格
  • 我可以将文件重命名为不包含空格并将其替换为破折号/下划线

然而,我个人很好奇如何做到这一点。这是我到目前为止所拥有的:

Redirect 301 /file_new.pdf http://sitename.com/file\ new.pdf

当我调用 configtest 时,conf 文件解析器会抛出错误:

重定向需要两个或三个参数, 可选状态,然后记录到 被重定向和目标 URL

编辑: 显然 mod_rewrite 或 Redirect 301 方法对我有用,可能是因为无论出于何种原因它们都没有应用,因为文件实际上存在于该位置。

<VirtualHost *:80>
DocumentRoot /www/sitename_com
ServerName site.local
Options -MultiViews

RewriteEngine on
Redirect 301 /pdf/file_new.pdf http://site.local/pdf/file%20new.pdf
RewriteRule ^/pdf/file_new.pdf http://site.local/pdf/file\ new.pdf
RewriteLog "/www/rewrite.log"
</VirtualHost>

在 rewrite.log 中,它尝试将模式与每个相应的 uri / http 请求进行匹配。我认为在到达 mod_alias/mod_rewrite 之前就已经有东西控制了。

所以总结一下,在浏览器中直接进入file_new.pdf,不会出现301。我确信 aliasrewrite 已启用。我的apache版本是2.2。

有帮助吗?

解决方案

Redirect 实际上是 mod_alias 的一部分,而不是 mod_rewrite 的一部分。我很高兴 %20 起作用了,您还可以使用引号告诉 apache 包含空格的路径是要重定向到的 URL,而不是两个单独的项目:

Redirect 301 /pdf/file_new.pdf "http://site.local/pdf/file new.pdf"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top