啟用 Apache Rewrite 重寫模塊的詳細(xì)過程
Apache 的 mod_rewrite 是最強(qiáng)大的 URL 操作模塊之一。使用 mod_rewrite,您可以重定向和重寫 url,這對于在您的網(wǎng)站上實現(xiàn) seo 友好的 URL 結(jié)構(gòu)特別有用。在本文中,我們將引導(dǎo)您了解如何在基于 Debian 和基于 RHEL 的系統(tǒng)上在 Apache 中啟用 mod 重寫。
檢查 mod_rewrite 是否啟用
在啟用 mod 重寫之前,檢查它是否已經(jīng)激活。
apache2ctl -M | grep rewrite
OR
httpd -M | grep rewrite
如果你看到 rewrite_module (shared),那么 mod 重寫已經(jīng)啟用。
開啟 mod_rewrite
根據(jù)您的操作系統(tǒng)在 Apache web 服務(wù)器中啟用 mod_rewrie 模塊。
On Debian-based Systems
(1) 安裝 Apache (如果尚未安裝)
sudo apt update sudo apt install apache2
(2) 啟用 mod_rewrite
sudo a2enmod rewrite
(3) 重啟 Apache
sudo systemctl restart apache2
On RHEL-based Systems
(1) 安裝 Apache (如果尚未安裝)
sudo yum install httpd
(2) mod_rewrite 模塊通常是默認(rèn)啟用的。如果沒有,可以通過編輯 Apache 配置手動加載它。
sudo nano /etc/httpd/conf/httpd.conf
(3) 確保下面一行存在并且沒有被注釋掉
LoadModule rewrite_module modules/mod_rewrite.so
(4) 重啟 Apache
sudo systemctl restart httpd
.htaccess 配置 mod_rewrite
要讓 mod_rewrite 規(guī)則 在 .htaccess 文件中工作,必須確保目錄配置允許重寫。
Apache 的配置文件位置:
- debian-based-systems: /etc/apache2/apache2.conf
- rhel-based-systems: /etc/httpd/conf/httpd.conf
找到您的網(wǎng)站根目錄并修改 AllowOverride 指令
<Directory /var/www/html> AllowOverride All </Directory>
在進(jìn)行更改之后,一定要記得重新啟動 Apache 服務(wù)。
測試 mod_rewrite
為了確保 mod_rewrite 能夠正常工作,你可以在 .htaccess 文件中設(shè)置一個基本規(guī)則
nano /var/www/html/.htaccess
添加以下內(nèi)容
RewriteEngine On RewriteRule ^hello\.html$ welcome.html [R=302,L]
創(chuàng)建一個 welcome.html 文件
echo "Welcome, TecAdmin!" > /var/www/html/welcome.html
訪問“http://your_server_ip/hello.html”應(yīng)該將您重定向到“http://your_server_ip/welcome.html”
到此這篇關(guān)于啟用 Apache Rewrite 重寫模塊的詳細(xì)過程的文章就介紹到這了,更多相關(guān)Apache Rewrite 重寫模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決VMware安裝linux系統(tǒng)啟動后黑屏問題
這篇文章主要介紹了解決VMware安裝linux系統(tǒng)啟動后黑屏問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12Linux如何修改hosts文件并刷新DNS生效hosts文件
這篇文章主要介紹了Linux如何修改hosts文件并刷新DNS生效hosts文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Linux下NOGUI快速使用Jmeter的兩種實現(xiàn)方式
文章介紹了在Linux環(huán)境下使用Jmeter進(jìn)行無GUI測試的兩種快速啟動方法:直接下載安裝包啟動和使用docker啟動,詳細(xì)描述了每種方法的步驟,并強(qiáng)調(diào)了執(zhí)行命令和查看報告的過程2025-02-02Apache實現(xiàn)禁止中文瀏覽器訪問與301重定向的方法
這篇文章主要介紹了Apache實現(xiàn)禁止中文瀏覽器訪問與301重定向的方法,通過修改.htaccess的配置來實現(xiàn)非常簡便,需要的朋友可以參考下2016-01-01