如何使用 htaccess 重定向目录,包括名称中带有空格的文件

发布于 2024-08-28 08:48:37 字数 521 浏览 4 评论 0原文

我正在处理这样一种情况,有人在服务器上给了我一堆旧文件,这些文件已经有很多直接指向它们的传入链接(主要是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必在意 2024-09-04 08:48:37

怎么样:

RedirectMatch permanent ^/manuals/(.*)$ http://www.domain.com/media/manual/$1

(.*) 匹配 URL 中 /manuals/ 后面的任何字符。

注意RedirectRedirectMatchmod_alias 的一部分,而不是 mod_rewrite(因为问题已标记)所以请原谅我之前发布的 mod_rewrite 规则。

What about:

RedirectMatch permanent ^/manuals/(.*)$ http://www.domain.com/media/manual/$1

The (.*) matches any character following /manuals/ in the URL.

Note: Redirect and RedirectMatch are part of mod_alias, not mod_rewrite (as the question was tagged) so forgive the earlier mod_rewrite rule I posted.

也只是曾经 2024-09-04 08:48:37

重定向可以同时作用于整个目录。

Redirect permanent /manuals/ http://www.domain.com/manuals/

然后您不必担心将空格转义为 %20 或任何其他字符。 Apache 会将该路径下的每个文件重定向到目标 URL 下的同一文件。

RedirectMatch 也存在用于正则表达式匹配;不过您不需要为此求助于它。)

Redirect can work on whole directories at once.

Redirect permanent /manuals/ http://www.domain.com/manuals/

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.)

莫相离 2024-09-04 08:48:37

如果您需要 URL 中的空格字符,可以使用 %20,这是空格的 URL 编码。

If you need characters for spaces in a URL, you can use %20 which is the URL Encoding of a space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文