我像这样编辑了我的主机文件。
example.com/abc/a.zip example.com/abc/b.ziP
所以我需要做的是,当我在浏览器中输入 example.com/abc/a.zip 网址时,它应该重定向到 example.com/abc/b.zip。但这不起作用。那么问题出在哪里?
还有其他方法可以做到这一点吗?
答案1
对于简单的重定向,这RewriteEngine
是多余的。只需将其添加到您的站点定义中:
Redirect /abc/a.zip http://example.com/abc/b.zip
看这个例子在 Apache 文档中。
答案2
重新映射 URL
如果您想重新映射url
到不同的位置,但客户端仍然可以访问并知道原始 URL,则应该使用RewriteRule
指令。
尝试添加以下行虚拟主机部分:
RewriteEngine on
RewriteRule ^/abc/a\.zip$ /abc/b.ziP [PT]
RewriteRule 的语法如下:
RewriteRule Pattern Substitution [flags]
根据 apachemod_rewrite 手册:
Pattern: In VirtualHost context, The Pattern will initially be matched
against the part of the URL after the hostname and port, and
before the query string (e.g. "/app1/index.html").
Substitution: A DocumentRoot-relative path to the resource to be served.
Note that mod_rewrite tries to guess whether you have specified a
file-system path or a URL-path by checking to see if the first
segment of the path exists at the root of the file-system.
For example, if you specify a Substitution string of
/www/file.html, then this will be treated as a URL-path unless
a directory named www exists at the root or your file-system
(or, in the case of using rewrites in a .htaccess file, relative
to your document root), in which case it will be treated as a
file-system path.
If you wish other URL-mapping directives (such as Alias) to be
applied to the resulting URL-path, use the [PT] flag as described below.
flag: special action
在这种情况下,标志值为 PT,意思是:
passthrough|PT Forces the resulting URI to be passed back to the URL
mapping engine for processing of other URI-to-filename translators,
such as Alias or Redirect. details ...
这里是带有示例的有用链接。
有别名指令也会重新映射内容位置。如果内容位置在 之外,则此指令很有用DocumentRoot
。
重定向请求
如果你想通知客户端请求的内容位于不同的 URL,并指示客户端使用新的 URL 发出新请求,你应该使用重定向指示。
尝试添加此行虚拟主机部分:
Redirect permanent /abc/a.zip http://example.com/abc/b.ziP
这有助于通知机器人内容已被移动到其他位置,并防止搜索引擎出现索引错误。