banner
阿珏酱

阿珏酱

乘上与平常相反的电车,去看看那未曾见过的风景
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

.htaccess偽靜態規則

提示:當你看到這個提示的時候,說明當前的文章是由原emlog博客系統搬遷至此的,文章發布時間已過於久遠,編排和內容不一定完整,還請諒解`

.htaccess 偽靜態規則

日期:2017-12-4 阿珏 折騰代碼 瀏覽:1701 次 評論:1 條

image Apache的 mod_rewrite是比較強大的,在進行網站建設時,可以通過這個模塊來實現偽靜態。

檢測 Apache 是否開啟 mod_rewrite 功能#

可以通過 php 提供的 phpinfo () 函數查看環境配置,找到 “Loaded Modules”,其中列出了所有 apache2handler 已經開啟的模塊,如果裡面包括 “mod_rewrite”,則已經支持,不再需要繼續設置。

如果沒有開啟 “mod_rewrite”,則打開目錄 apache 目錄下的 “/apache/conf/”,找到 httpd.conf 文件,再找到 “LoadModule rewrite_module”,將前面的”#” 號刪除即表示取用該功能。

如果沒有查找到 “LoadModule” 區域,可以在最後一行加入 “LoadModule rewrite_module ,modules/mod_rewrite.so”(獨佔一行),之後重啟 apache 服務器。
再通過 phpinfo () 函數查看環境配置就有 “mod_rewrite” 為項了.。

讓 apache 服務器支持.htaccess#

如何讓自己的本地 APACHE 服務器支持:“htaccess” 呢?只需修改 apache 的 httpd.conf 設置就可以讓 APACHE 支持 “.htaccess” 了。打開 APACHE 目錄的 CONF 目錄下的 httpd.conf 文件,找到: Options FollowSymLinks AllowOverride None 改為 Options FollowSymLinks AllowOverride All 就行了。

建立.htaccess 文件#

建立.htaccess 文件時要注意,不能直接建,方法是通過記事本中的另存為菜單,在文件名窗口輸入:“.htaccess”,然後點擊保存。

rewrite 規則學習#

RewriteEngine on #rewriteengine 為重寫引擎開關 on 為開啟 off 為關閉

RewriteRule ([0-9]{1,})$index.php?id=$1 在這裡,RewriteRule 是重寫規則,是用正則表達式的句子,([0-9]{1,}) 表示由數字組成的,$ 表示結束標誌,表示以數字結束!

如果要實現偽靜態頁面,規則如下:

RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2

以下是我博客的 .htaccess#

<IfModule mod_rewrite.c>
RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^post-([0-9]{1,}).html$ index.php?post=$1 
</IfModule>

http 強制轉跳 https 的規則#

RewriteEngine on
RewriteBase / 
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

網友評論:

image 廣東自考網 6 個月前 (2020-08-28)
學習到了 感謝博主的分享

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。