apache - mod_rewrite配置指令RewriteBase

本文详细解析了Apache中RewriteBase指令的作用与使用方法,帮助读者理解如何设置正确的URL前缀,确保重写规则能够正确地应用于网站路径。

看了好几遍,还是不太清楚.先记下了,有空再看看.apache发展了这么多年,我也用了这么久,就这样一个指令,会用但是不完全了解实际的含义.

RewriteBase Directive

Description:Sets the base URL for per-directory rewrites
Syntax:RewriteBase URL-path
Default:See usage for information.
Context:directory, .htaccess
Override:FileInfo
Status:Extension
Module:mod_rewrite

The RewriteBase directive explicitly sets the base URL for per-directory rewrites. As you will see below, RewriteRule can be used in per-directory config files (.htaccess). In such a case, it will act locally, stripping the local directory prefix before processing, and applying rewrite rules only to the remainder. When processing is complete, the prefix is automatically added back to the path. The default setting is; RewriteBase physical-directory-path

When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.

If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.

For example, assume the following per-directory config file:

#
#  /abc/def/.htaccess -- per-dir config file for directory /abc/def
#  Remember: /abc/def is the physical path of /xyz, i.e., the server
#            has a 'Alias /xyz /abc/def' directive e.g.
#

RewriteEngine On

#  let the server know that we were reached via /xyz and not
#  via the physical path prefix /abc/def
RewriteBase   /xyz

#  now the rewriting rules
RewriteRule   ^oldstuff/.html$  newstuff.html

In the above example, a request to /xyz/oldstuff.html gets correctly rewritten to the physical file /abc/def/newstuff.html.

For Apache Hackers

The following list gives detailed information about the internal processing steps:

Request:
  /xyz/oldstuff.html

Internal Processing:
  /xyz/oldstuff.html     -> /abc/def/oldstuff.html  (per-server Alias)
  /abc/def/oldstuff.html -> /abc/def/newstuff.html  (per-dir    RewriteRule)
  /abc/def/newstuff.html -> /xyz/newstuff.html      (per-dir    RewriteBase)
  /xyz/newstuff.html     -> /abc/def/newstuff.html  (per-server Alias)

Result:
  /abc/def/newstuff.html

This seems very complicated, but is in fact correct Apache internal processing. Because the per-directory rewriting comes late in the process, the rewritten request has to be re-injected into the Apache kernel. This is not the serious overhead it may seem to be - this re-injection is completely internal to the Apache server (and the same procedure is used by many other operations within Apache).

<think>我们正在配置Apachemod_rewrite模块以实现从www1跳转到www2。根据用户需求,我们需要在虚拟主机配置中启用重写规则。根据引用,我们注意到配置虚拟主机通常在httpd-vhosts.conf文件中进行。同时,要使用mod_rewrite,需要确保该模块已加载,并在相应目录中允许重写覆盖(AllowOverride)。步骤:1.确保mod_rewrite模块已启用。在Apache配置中(通常是httpd.conf)查找以下行,确保没有被注释:LoadModulerewrite_modulemodules/mod_rewrite.so2.配置虚拟主机。根据引用[1]和[2],我们可以在httpd-vhosts.conf文件中配置虚拟主机,并在<Directory>段中设置AllowOverrideAll以允许使用.htaccess文件中的重写规则,或者直接在虚拟主机配置中编写重写规则。3.用户要求从www1跳转到www2,因此我们需要为www1配置重写规则,将所有请求重定向到www2。有两种方法:a)在虚拟主机配置中直接使用RewriteEngine和RewriteRule指令。b)在网站根目录的.htaccess文件中配置(需要AllowOverrideAll)。这里我们采用在虚拟主机配置中直接配置的方法,因为这样效率更高。配置示例(假设我们有两个虚拟主机,一个用于www1,一个用于www2):-我们可以在www1的虚拟主机配置中设置重定向规则,将所有请求重定向到www2。注意:用户没有提供具体的域名,所以我们假设为www1.com和www2.com。配置如下:首先,确保有一个虚拟主机监听www1.com的请求,然后在该虚拟主机中设置重写规则:```<VirtualHost*:80>ServerNamewww1.comServerAliaswww1#开启重写引擎RewriteEngineOn#将所有请求重定向到www2.com,保留原始请求的路径和查询字符串RewriteRule^(.*)$http://www2.com$1[R=301,L]#其他配置,如日志等ErrorLog"logs/www1-error_log"CustomLog"logs/www1-access_log"common</VirtualHost>```同时,确保www2.com的虚拟主机已经配置好,用于接收来自www1的重定向请求。4.在<Directory>配置中,根据引用[1]和[2],我们需要设置AllowOverride。如果我们在虚拟主机中直接配置了重写规则,那么AllowOverride可以设置为None(或No)以提升性能。但如果用户希望使用.htaccess,则需要设置为All。由于我们在虚拟主机中直接配置了重写规则,所以<Directory>段可以这样设置(如果存在):```<Directory"/path/to/www1/document/root">#因为我们不在.htaccess中使用重写,所以AllowOverride可以设置为NoneAllowOverrideNoneRequireallgranted</Directory>```5.保存配置文件后,重启Apache服务。注意:如果用户使用的是基于名称的虚拟主机,确保NameVirtualHost已经配置(在较新的Apache版本中通常不需要)。另外,如果用户使用的是RPM安装的Apache配置文件的位置可能有所不同,常见的路径是/etc/httpd/conf/httpd.conf,虚拟主机配置文件可能在/etc/httpd/conf.d/目录下。由于用户提到的是rpm安装,所以我们需要考虑这些路径差异。具体步骤:-检查模块是否加载:在/etc/httpd/conf/httpd.conf中查找LoadModulerewrite_module,如果没有则添加或取消注释。-在虚拟主机配置文件中(例如/etc/httpd/conf.d/vhost.conf)添加上述虚拟主机配置。最后,重启Apache服务:systemctlrestarthttpd#或者servicehttpdrestart相关问题:1.如何测试mod_rewrite模块是否已启用?2.除了301重定向,还有哪些重定向方式?3.如何配置多个域名的重定向规则?4.重写规则中的[R=301,L]标志分别代表什么?</think>### Apache mod_rewrite 配置域名跳转指南 要配置 Apachemod_rewrite 模块实现 `www1` 跳转到 `www2`,请按以下步骤操作: #### 步骤 1:启用 mod_rewrite 模块 ```bash # 查找模块配置文件(RPM安装路径) sudo vi /etc/httpd/conf.modules.d/00-base.conf # 确保以下行未被注释 LoadModule rewrite_module modules/mod_rewrite.so ``` #### 步骤 2:配置虚拟主机 在虚拟主机配置文件(如 `/etc/httpd/conf.d/vhost.conf`)中添加: ```apache <VirtualHost *:80> ServerName www1.com DocumentRoot /var/www/html/www1 <Directory "/var/www/html/www1"> Options Indexes FollowSymLinks AllowOverride All # 关键:允许.htaccess重写规则生效 Require all granted </Directory> # 重写规则配置 RewriteEngine On RewriteCond %{HTTP_HOST} ^www1\.com$ [NC] RewriteRule ^(.*)$ http://www2.com$1 [R=301,L] </VirtualHost> <VirtualHost *:80> ServerName www2.com DocumentRoot /var/www/html/www2 # 其他配置... </VirtualHost> ``` **关键参数说明**: - `AllowOverride All`:启用目录级重写规则(必须设置)[^1][^2] - `RewriteEngine On`:激活重写引擎 - `NC`:不区分大小写匹配 - `R=301`:永久重定向(SEO友好) - `L`:最后一条规则 #### 步骤 3:创建.htaccess文件(可选) 在 `www1` 的网站根目录(如 `/var/www/html/www1`)创建 `.htaccess`: ```apache RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www1\.com$ [NC] RewriteRule ^(.*)$ http://www2.com/$1 [R=301,L] ``` #### 步骤 4:重启Apache服务 ```bash sudo systemctl restart httpd sudo apachectl configtest # 先测试配置语法 ``` #### 验证方法 1. 访问 `http://www1.com/example` 应自动跳转到 `http://www2.com/example` 2. 使用curl测试: ```bash curl -I http://www1.com # 应返回 HTTP/1.1 301 Moved Permanently # Location: http://www2.com/ ``` ### 注意事项 1. **防火墙设置**:确保防火墙开放80端口 ```bash sudo firewall-cmd --add-service=http --permanent sudo firewall-cmd --reload ``` 2. **DNS解析**:需在DNS服务器将 `www1.com` 和 `www2.com` 解析到Apache服务器IP 3. **SELinux**:若启用需更新文件上下文: ```bash sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html/www1(/.*)?" sudo restorecon -Rv /var/www/html ``` ### 常见问题排查 - **重写规则不生效**:检查`AllowOverride All`是否配置正确[^1][^2] - **403禁止访问**:确保`Require all granted`已设置[^1][^3] - **500服务器错误**:检查重写规则语法,用`apachectl configtest`验证 > 配置示例参考Apache虚拟主机文档[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值