在本文中,我们将学习如何在CentOS 7上为Apache配置mod_rewrite,Apache是一种非常流行的Web服务器,它允许我们自定义和启用其功能并禁用它们。如果现在需要更多资源,则Apache管理员有权根据Web应用程序的需求进行更改。
我们需要一台装有CentOS 7的Linux机器。
我们需要一个具有sudo权限的非root用户。
我们将使用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,因此要进行验证,我们将使用带有-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文件将定义Apache Web服务器的指令,该指令包括域上的RewriteRules而不更改Linux中的配置文件,并且将以dot(。)开头的文件视为隐藏文件。
我们需要在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编辑器。
RewriteCond是一个指令,如果启用了rewiteCond,则Apache将遵循RewriteRule。
apache将处理无效的Web步速或URL的请求,它将显示“ 404页面未找到”,而不是显示404错误页面,我们可以将URL重定向回首页或其他Web页面。
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^admin/(.*)$ /home
我们可以使用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服务器使用该重定向并将流量重定向到其他站点或有条件或无条件的其他位置。