htaccess 關於重寫和重定向的問題

htaccess 關於重寫和重定向的問題

我一直在用頭撞牆,試圖在 .htaccess 中做一些應該相當簡單的事情,但沒有成功。我希望有人能幫助我。

我需要實現3 件基本事情: 1)將我的網域www.sample.com 重新導向到www.sample.com/subdir1/cgi-bin/ 2)如果頁面為NULL 或index.html,則載入腳本home. php 3 ) 隱藏子目錄“subdir1/cgi-bin”

例如,當有人造訪 www.sample.com 時,他們會看到:www.sample.com/home.php

伺服器實際上會讀取 www.sample.com/subdir1/cgi-bin/home.php

謝謝你!

答案1

您可以擁有一個如下所示的 .htaccess 檔案:

Options +FollowSymLinks
IndexIgnore */*

# Turn on the RewriteEngine
RewriteEngine On

# When accessing root
DirectoryIndex subdir1/cgi-bin/home.php

# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . subdir1/cgi-bin/home.php

然後你必須確保你的 Apache 伺服器授權重寫。要實現此目的,請編輯文件/etc/apache2/apache2.conf。在此文件中,找到您使用的目錄並將“AllowOverride None”更改為“AllowOverride all”。這是一個例子:

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

之後,透過執行以下命令來啟動 Apache 的重寫模組:

sudo a2enmod rewrite

最後,重新啟動 Apache:

sudo service apache2 restart

相關內容