如何在 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
# http://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 流量到其他站點或其他位置(有或無條件)的重要指令。

更新於:2020 年 1 月 21 日

1K+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告