.htaccess 中的重写规则
我想将链接“http://blog.test.com/”更改为“http://www.test.com/blog/”。
我在 .htaccess 中尝试了以下代码
RewriteRule ^blog.test.com?$ test.com/blog [NC,R=301,L]
我错过了什么吗?谢谢
I would like to change the link "http://blog.test.com/" to "http://www.test.com/blog/".
I've tried the following code in my .htaccess
RewriteRule ^blog.test.com?$ test.com/blog [NC,R=301,L]
Did I miss anything? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 apache,则需要在 RewriteCond 中匹配 url 的主机部分(例如 blog.test.com):
If you're using apache, you need to match the host part of the url (e.g. blog.test.com) in a RewriteCond:
首先,您必须在 HTML href 中将
http://blog.test.com/whatever_or_empty
替换为http://www.test.com/blog/whatever_or_empty
。blog.test.com
虽然是子域,但却是不同的 URL。即,当 RewriteRule 重写另一个 URL 时,将发生外部重定向。这将反映在浏览器中。为临时重定向(302(默认))
或永久重定向(301)
。因此,使用 url 重写将链接
http://blog.test.com/
更改为http://www.test.com/blog/
是没有用的。不过,您可以使用 Apache 模块 mod_proxy 来实现此目的。
Apache 代理模块有这些:
您至少需要启用
mod_proxy
和mod_proxy_http
模块才能使代理正常工作:您的 apache 的
conf
文件中应该包含与这些类似的行:在您的
http://www.test.com
的Virtualhost
中使用此定义:
您还可以通过
mod_cache
使用缓存:mod_cache
。有关缓存的更多信息,请参阅此处:mod_cache Apache Docs< /a>.
first of all, you must replace
http://blog.test.com/whatever_or_empty
tohttp://www.test.com/blog/whatever_or_empty
in your HTML hrefs.blog.test.com
although a sub domain, is a different URL. i.e. when a RewriteRule does a rewrite to another URL an external redirect will occur. This will reflect in the browser. Be a temporaryredirect(302(the default))
orpermanent redirect(301)
.So, using url rewriting to change the link
http://blog.test.com/
tohttp://www.test.com/blog/
is useless.Although, you can achieve this using Apache Module mod_proxy.
The Apache Proxy Modules has these:
You need at-least
mod_proxy
andmod_proxy_http
modules enabled for the proxy to work:you should have lines similar to these in your apache's
conf
file:use this in your
Virtualhost
ofhttp://www.test.com
Definitions:
You can also use a cache with
mod_cache
:mod_cache
.For more on caching, refer here: mod_cache Apache Docs.