有一個域名。比方說domain.com
。如何將網站重定向到特定文件?
當用戶只鍵入domain.com
時,他應該被重定向到地址domain.com/file.html
。我怎麼能用.htaccess
文件做到這一點?
另外,RewriteRule ^index\.php$ - [L]
是什麼意思?它會幫助我嗎?
有一個域名。比方說domain.com
。如何將網站重定向到特定文件?
當用戶只鍵入domain.com
時,他應該被重定向到地址domain.com/file.html
。我怎麼能用.htaccess
文件做到這一點?
另外,RewriteRule ^index\.php$ - [L]
是什麼意思?它會幫助我嗎?
添加到您的.htaccess文件
DirectoryIndex file.html
看看這個網站:.htaccess tricks and tips它是重寫規則一個很好的參考。
至於RewriteRule^index.php $ - [L]是什麼意思。這將忽略與結束Rewrite Rule meaning
我寫了DirectoryIndex file.html,但是當index.php時,它也重定向到file.html –
是你的.htaccess文件中唯一的條目(RewriteRule^index.php $ - [L])嗎? – Robert
即使我刪除了所有htaccess代碼,它仍然會重定向到domain.com,當您鍵入domain.com/index.php並且index.php文件中沒有任何重定向它時。 –
的index.php的東西要重定向你可以做一個索引頁(第一頁,用戶訪問)
DirectoryIndex file.html
根據本link修改.htaccess文件。
對於RewriteRule
你需要能夠在Apache中的mod_rewrite模塊,然後在您的.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) file.html [L]
</IfModule>
在RewriteRule
你可以把正則表達式來識別網址和用戶重定向到一個文件你想(在這種情況下,你會重定向你的所有鏈接到file.html
)。更多信息here。
而且可能是您有一個配置:
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml
</IfModule>
所以,在默認情況下,如果您在您的domain.com的根目錄有一個叫index.php
文件始終該文件將首先被調用。
嘗試把在位於domain.com
RewriteEngine On
RewriteBase/
#put all the following rules before any other rules in the .htaccess file
RewriteRule ^file\.html$ - [L]
#domain.com or any subdomain
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
#the root of domain
RewriteCond %{REQUEST_URI} ^/?$ [NC]
#return file.html
RewriteRule . file.html [L]
什麼的index.php文件的根.htaccess文件下面?如果我寫index.php文件,它會重定向到file.html –
@hey - 不,它不會。你有沒有嘗試過? –
@ÁlvaroG. Vicario:是的,我已經嘗試過,它可以工作。也許它是託管的問題?因爲它總是重定向到domain.com,當你添加index.php時,即使沒有DirectoryIndex。 –