如何在 CentOS 7 上配置 Apache 的 mod_rewrite
在本文中,我们将学习如何在 CentOS 7 上配置 Apache 的 mod_rewrite。Apache 是一款非常流行的 Web 服务器,它允许我们自定义和启用其功能,并禁用它们。如果我们现在需要更多功能,Apache 管理员有权根据我们的 Web 应用程序需求进行更改。
先决条件
- 我们需要一台安装了 CentOS 7 的 Linux 计算机。
- 我们需要一个具有 sudo 权限的非 root 用户。
安装 Apache Web 服务器
我们将使用 yum 安装 Apache Web 服务器,因为 Apache 在 CentOS 存储库的管理实用程序提供的默认软件包中可用。
Apache 守护进程在 CentOS 中称为 httpd,我们需要使用 yum 命令安装 Apache,因为这是 CentOS 软件包管理实用程序提供的默认软件包。
以下是安装 Apache Web 服务器的命令
$ sudo yum install httpd Output: sudo yum install httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.fibergrid.in * extras: mirror.fibergrid.in * updates: mirror.fibergrid.in Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed --> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-3.el7 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed ---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================= Package Arch Version Repository Size ================================================================================================= Installing: httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M Installing for dependencies: apr x86_64 1.4.8-3.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ================================================================================================= Install 1 Package (+4 Dependent packages) Total download size: 3.0 M Installed size: 10 M Is this ok [y/d/N]: y Downloading packages: (1/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (2/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:00 (3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00 (4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:00 (5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:01 ------------------------------------------------------------------------------------------------- Total 2.4 MB/s | 3.0 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-3.el7.x86_64 1/5 Installing : apr-util-1.5.2-6.el7.x86_64 2/5 Installing : httpd-tools-2.4.6-40.el7.centos.4.x86_64 3/5 Installing : mailcap-2.1.41-2.el7.noarch 4/5 Installing : httpd-2.4.6-40.el7.centos.4.x86_64 5/5 Verifying : httpd-tools-2.4.6-40.el7.centos.4.x86_64 1/5 Verifying : apr-1.4.8-3.el7.x86_64 2/5 Verifying : mailcap-2.1.41-2.el7.noarch 3/5 Verifying : httpd-2.4.6-40.el7.centos.4.x86_64 4/5 Verifying : apr-util-1.5.2-6.el7.x86_64 5/5 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7 Complete!
软件包安装完成后,我们需要使用 systemctl 命令实用程序启动 Apache 守护进程。
$ sudo systemctl start httpd
httpd 守护进程启动后,我们将使用以下命令检查 httpd 服务的状态 –
$ sudo systemctl status httpd Output: httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2016-11-09 12:52:17 IST; 4s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 29600 (httpd) Status: "Processing requests..." CGroup: /system.slice/httpd.service ├─29600 /usr/sbin/httpd -DFOREGROUND ├─29601 /usr/sbin/httpd -DFOREGROUND ├─29602 /usr/sbin/httpd -DFOREGROUND ├─29603 /usr/sbin/httpd -DFOREGROUND ├─29604 /usr/sbin/httpd -DFOREGROUND └─29605 /usr/sbin/httpd -DFOREGROUND Nov 09 12:52:17 localhost.localdomain systemd[1]: Starting The Apache HTTP Server... Nov 09 12:52:17 localhost.localdomain httpd[29600]: AH00558: httpd: Could not reliably deter...ge Nov 09 12:52:17 localhost.localdomain systemd[1]: Started The Apache HTTP Server. Hint: Some lines were ellipsized, use -l to show in full.
验证 mod_rewrite 指令
由于默认情况下mod_rewrite已启用,因此要进行验证,我们将使用带有 -t -D DUMP_RUN_CFG 选项的 httpd 命令实用程序运行以下命令,该命令将显示当前配置加载的所有模块。
$ httpd -t -D DUMP_RUN_CFG Output: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message ServerRoot: "/etc/httpd" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/etc/httpd/logs/error_log" Mutex default: dir="/run/httpd/" mechanism=default Mutex mpm-accept: using_defaults Mutex authdigest-opaque: using_defaults Mutex proxy-balancer-shm: using_defaults Mutex rewrite-map: using_defaults Mutex authdigest-client: using_defaults Mutex proxy: using_defaults Mutex authn-socache: using_defaults PidFile: "/run/httpd/httpd.pid" Define: DUMP_RUN_CFG User: name="apache" id=48 Group: name="apache" id=48
我们可以编辑配置文件以启用或禁用 rewrite_modules。
$ sudo vi /etc/httpd/conf.modules.d/00-base.conf Output: # # This file loads most of the modules included with the Apache HTTP # Server itself. # LoadModule access_compat_module modules/mod_access_compat.so LoadModule actions_module modules/mod_actions.so LoadModule alias_module modules/mod_alias.so LoadModule allowmethods_module modules/mod_allowmethods.so LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule auth_digest_module modules/mod_auth_digest.so LoadModule authn_anon_module modules/mod_authn_anon.so …. … … LoadModule rewrite_module modules/mod_rewrite.so …. … …
启用 rewrite_module 后,我们需要保存文件并退出
我们需要通过重新启动 Apache 服务器来应用所做的配置更改
$ sudo systemctl restart httpd
配置 .htaccess 文件
.htaccess 文件将定义 Apache Web 服务器的指令,其中包括域上的 RewriteRules,而无需更改 Linux 中的配置文件,并且以点(.)开头的文件被视为隐藏文件。
我们需要在 Apache 指令中编辑并添加 AllowOverride 设置。
$ sudo nano .etc.httpd/conf.httpd.conf Output: … … # Further relax access to the default document root: <Directory "/var/www/html"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # https://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride ALL # # Controls who can get stuff from this server. # Require all granted </Directory> .. ..
保存配置文件并使用以下命令应用更改 –
$ sudo systemctl restart httpd
在 .htaccess 文件中添加以下行以激活 RewriteEngine。
$ sudo vi /var/www/html/.htaccess RewriteEngine On
保存文件并退出 vi 编辑器。
RewriteEngine 条件与逻辑
如果启用了 rewriteCond,则 RewriteCond 是一个用于指示 Apache 遵循 RewriteRule 的指令。
对于无效的网页或 URL 请求,apache 将显示 404 页面未找到,而不是显示 404 错误页面,我们可以将 URL 重定向回主页或其他网页。
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^admin/(.*)$ /home
IP 地址限制
我们可以使用 Rewite Condition 允许或阻止来自特定 IP 地址的流量。
为了演示,以下代码是我们要阻止所有流量并接受来自一个地址的流量。
RewriteCond %{REMOTE_ADDR} !^(198\.168\.100\.20)$ RewriteRule (.*) - [F,L]
使用的选项。
%{REMOTE_ADDR} - is the address string. !^(198\.168\.100\.20)$ - the IP address where to allow the web traffic. F – Flage deny the access L – Which indicates the last rules to run.
在本文中,我们已在 Apache 上启用了 RewriteRules 和 RewriteCond,我们希望在有或没有条件的情况下重定向流量,并且 mod_rewrite 也是 Apache Web 服务器使用和重定向 Web 流量到其他站点或其他位置(有或无条件)的重要指令。