如何使用 htaccess 重定向目录,包括名称中带有空格的文件
我正在处理这样一种情况,有人在服务器上给了我一堆旧文件,这些文件已经有很多直接指向它们的传入链接(主要是 pdf 文件)。我现在已组织好文件,但位于不同的目录中。之前是“域/手册/文件”,现在是“域/媒体/手册/文件”。我正在尝试使用 htaccess 文件解决此问题。
许多文件的名称中都有空格(不是我可以控制的),并且因为它们已经有指向它们的链接,所以我不能只是重命名它们。我发现当文件名称中包含空格时,我可以使用引号单独重定向文件,例如:
redirect 301 "/manuals/file 123.pdf" "http://www.domain.com/manuals/file 123.pdf"
但是,这些文件有很多,我想知道是否有一种方法可以创建一个正则表达式来处理文件名中的空格我可以用它来重定向整个目录。我应该补充一点,有些文件包含正确的文件名,其中没有空格,有些文件有一个空格,有些文件有多个空格。这并不漂亮。
如果您之前遇到过这个问题,我真的很感激听到您的建议,我已经没有主意了。
谢谢。
I am dealing with a situation where someone has handed me a bunch of old files on the server which already have a lot of incoming links directly to them (mostly pdf files). I now have the files organized but in a different directory. Before it was 'domain/manuals/file' now it is 'domain/media/manual/file'. I am trying to resolve this issue using an htaccess file.
Many of the files have spaces in the names (not something I can control) and because they already have links to them I can't just go through renaming them. I have found that I can redirect files individually when they have spaces in the names by using quotes such as:
redirect 301 "/manuals/file 123.pdf" "http://www.domain.com/manuals/file 123.pdf"
However, there are loads of these files and I wondered if there is a way to create a regular expression that will handle spaces in file names that I could use to redirect the entire directory. I should add that some files contain decent file names with no spaces in, some have one space and others more than one space. It's not pretty.
If you've encountered this problem before I would really appreciate hearing your advice, I'm running out of ideas.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
(.*)
匹配 URL 中/manuals/
后面的任何字符。注意:
Redirect
和RedirectMatch
是mod_alias
的一部分,而不是mod_rewrite
(因为问题已标记)所以请原谅我之前发布的mod_rewrite
规则。What about:
The
(.*)
matches any character following/manuals/
in the URL.Note:
Redirect
andRedirectMatch
are part ofmod_alias
, notmod_rewrite
(as the question was tagged) so forgive the earliermod_rewrite
rule I posted.重定向可以同时作用于整个目录。
然后您不必担心将空格转义为
%20
或任何其他字符。 Apache 会将该路径下的每个文件重定向到目标 URL 下的同一文件。(
RedirectMatch
也存在用于正则表达式匹配;不过您不需要为此求助于它。)Redirect can work on whole directories at once.
Then you don't have to worry about escaping spaces to
%20
, or any other character. Apache will redirect every file under that path to the same file under the destination URL.(
RedirectMatch
exists as well for regex matching; you don't need to resort to that for this though.)如果您需要 URL 中的空格字符,可以使用
%20
,这是空格的 URL 编码。If you need characters for spaces in a URL, you can use
%20
which is the URL Encoding of a space.