客戶常常有一種情況,把論壇或是CMS的主程式,灌在public_html這個資料夾下的某個子資料夾,例如bbs,之後又提交服務單給身為主機商的我,希望他的網址,可以是 http://example.com/index.php 而不是 http://example.com/bbs/index.php 簡單的說,就是希望可以做apache mod_rewrite把子資料夾(subdirectory)的路徑去掉。
要做這件事,步驟如下:
(1) 在public_html下,新增一個.htaccess檔
(2) 在.htaccess檔案,複製貼上,下方的內容。並且替換example.com為客戶的主域名,替換subdirectory為客戶的子資料夾,替換index.html為子資料夾裡的索引檔。
# UnetHost.com
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change ‘subdirectory’ to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don’t change these line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change ‘subdirectory’ to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change ‘subdirectory’ to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]