1

我想在 .htaccess 中为我的网站创建一个重写,以便当用户请求 URL A 时,内容来自 URL B,但用户仍将 URL 视为 URL A。

例如,假设我在 mydomain.com/projects/project-example 有内容。我希望用户能够请求 mydomain.com/project-example,仍然在他们的地址栏中看到该 URL,但浏览器应该显示来自 mydomain.com/projects/project-example 的内容。

我已经查看了几个 .htaccess 重写技巧和常见问题解答,但不幸的是,它们似乎都没有为我上面描述的内容提供解决方案。并非我域中的所有内容都来自 /projects/ 目录,因此我认为重写应该首先检查页面是否存在,因此它不会将 /projects/ 附加到每个 url。我真的很难过。

如果重写不是我所需要的,或者这个问题有一个简单的解决方案,我很想听听。

4

1 回答 1

1

本教程应该有你需要的一切,包括准确地解决你的要求:http ://httpd.apache.org/docs/2.0/misc/rewriteguide.html 。这可能只是一个术语问题。

例如,假设我在 mydomain.com/projects/project-example 有内容。我希望用户能够请求 mydomain.com/project-example,仍然在他们的地址栏中看到该 URL,但浏览器应该显示来自 mydomain.com/projects/project-example 的内容。

有类似的东西:

RewriteRule ^project-example$ /projects/project-example [L]

当有人请求http://mydomain.com/project-example并且 URI/project-example在内部被重写为/projects/project-example. 请注意,当它位于 .htaccess 文件中时,URI/project-example会在匹配时删除前导斜杠。

如果你有一个东西目录,你可以使用正则表达式和反向引用,例如你希望http://mydomain.com/stuff/的任何请求映射到/internal/stuff/

RewriteRule ^stuff/(.*)$ /internal/stuff/$1 [L]

因此,对http://mydomain.com/stuff/file1.htmlhttp://mydomain.com/stuff/image1.png等的请求会被重写为/internal/stuff/file1.html,/internal/stuff/image1.png等。

于 2012-05-09T20:33:12.760 回答