.htaccess 中的重写规则

发布于 2025-01-03 16:33:08 字数 197 浏览 1 评论 0原文

我想将链接“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 技术交流群。

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

发布评论

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

评论(2

日暮斜阳 2025-01-10 16:33:08

如果您使用 apache,则需要在 RewriteCond 中匹配 url 的主机部分(例如 blog.test.com):

RewriteCond %{HTTP_HOST} ^blog.test.com$ [NC]
RewriteRule ^(.*)$ http://www.test.com/blog/$1  [R=301,L]

If you're using apache, you need to match the host part of the url (e.g. blog.test.com) in a RewriteCond:

RewriteCond %{HTTP_HOST} ^blog.test.com$ [NC]
RewriteRule ^(.*)$ http://www.test.com/blog/$1  [R=301,L]
初吻给了烟 2025-01-10 16:33:08

首先,您必须在 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:它处理通过 HTTP 和 HTTPS 获取文档。
  • mod_proxy_ftp:它处理通过 FTP 获取文档。
  • mod_proxy_connect:它处理安全 (SSL) 隧道的 CONNECT 方法。
  • mod_proxy_ajp:它处理 Tomcat 和类似后端服务器的 AJP 协议。
  • mod_proxy_balancer 在多个后端上实现集群和负载平衡。
  • mod_cache、mod_disk_cache、mod_mem_cache:这些处理管理文档缓存。要启用缓存,需要 mod_cache 以及 disk_cache 和 mem_cache 之一或两者。
  • mod_proxy_html:这会将 HTML 链接重写到代理的地址空间中。
  • mod_xml2enc:这支持代表 mod_proxy_html 和其他标记过滤模块的国际化 (i18n)。空间。
  • mod_headers:这会修改 HTTP 请求和响应标头。
  • mod_deflate:与客户端和后端协商压缩。

您至少需要启用 mod_proxymod_proxy_http 模块才能使代理正常工作:

您的 apache 的 conf 文件中应该包含与这些类似的行:

LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so

在您的 http://www.test.comVirtualhost 中使用此

ProxyPass /blog http://blog.test.com
ProxyPassReverse /blog http://blog.test.com
ProxyRequests On
ProxyVia On

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

定义:


您还可以通过 mod_cache 使用缓存:mod_cache
有关缓存的更多信息,请参阅此处:mod_cache Apache Docs< /a>.

first of all, you must replace http://blog.test.com/whatever_or_empty to http://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 temporary redirect(302(the default)) or permanent redirect(301).

So, using url rewriting to change the link http://blog.test.com/ to http://www.test.com/blog/ is useless.


Although, you can achieve this using Apache Module mod_proxy.

The Apache Proxy Modules has these:

  • mod_proxy: The core module deals with proxy infrastructure and configuration and managing a proxy request.
  • mod_proxy_http: This handles fetching documents with HTTP and HTTPS.
  • mod_proxy_ftp: This handles fetching documents with FTP.
  • mod_proxy_connect: This handles the CONNECT method for secure (SSL) tunneling.
  • mod_proxy_ajp: This handles the AJP protocol for Tomcat and similar backend servers.
  • mod_proxy_balancer implements clustering and load-balancing over multiple backends.
  • mod_cache, mod_disk_cache, mod_mem_cache: These deal with managing a document cache. To enable caching requires mod_cache and one or both of disk_cache and mem_cache.
  • mod_proxy_html: This rewrites HTML links into a proxy's address space.
  • mod_xml2enc: This supports internationalisation (i18n) on behalf of mod_proxy_html and other markup-filtering modules. space.
  • mod_headers: This modifies HTTP request and response headers.
  • mod_deflate: Negotiates compression with clients and backends.

You need at-least mod_proxy and mod_proxy_http modules enabled for the proxy to work:

you should have lines similar to these in your apache's conf file:

LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so

use this in your Virtualhost of http://www.test.com

ProxyPass /blog http://blog.test.com
ProxyPassReverse /blog http://blog.test.com
ProxyRequests On
ProxyVia On

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

Definitions:


You can also use a cache with mod_cache: mod_cache.
For more on caching, refer here: mod_cache Apache Docs.

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