Apache 透過 htaccess 隱藏 php 副檔名(mod_rewrite)

本教學使用環境介紹
伺服器端:Ubuntu 18.04 LTS
資料庫:Mariadb 10.1.34(Mysql)
語言版本:php 7.3
本機端:MacOS High Sierra

首先安裝好 apache 後,啓用 rewrite 功能

$ a2enmod rewrite

然後再到 /etc/apache2/apache2.conf 添加內容

$ nano /etc/apache2/apache2.conf

內容爲

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

記得將路徑設爲你的網站根目錄路徑呦!

接着將 apache reload

service apache2 reload

然後去你的網站根目錄中添加檔案爲 .htaccess,並加入以下內容

Options -Indexes
Options +FollowSymlinks
RewriteEngine on

在同一個 .htaccess 檔案中添加 RewriteRule,就像這樣

Options -Indexes
Options +FollowSymlinks
RewriteEngine on

RewriteRule ^admin/index$ admin/index.php

這樣只要訪問路徑 /admin/index 就等於是訪問了 /admin/index.php,apache已經幫你隱藏副檔名了

Line ID:ianmac
QQ:1258554508

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章