簡體   English   中英

單頁.htaccess重寫

[英]Single Page .htaccess Rewrite

我想使用htaccess重寫來顯示沒有擴展名的單個頁面。

http://site.com/accounthttp://site.com/account.php

RewriteEngine on
RewriteRule ^account$ /account.php [L]

這不行嗎? 它什么也沒做。

我也嘗試了一個普通的方法,但是那也不起作用:

RewriteEngine on
RewriteRule ^(.*)/$ $1.php

我的建議是使用動態規則,而不是靜態規則:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

這樣,任何頁面都可以引用任何PHP腳本。

例如:

http://site.com/account將加載http://site.com/account.php

http://site.com/login將加載http://site.com/login.php

但是,如果您確實喜歡使用靜態重寫規則,則可以使用以下內容:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^index index.php [L]

我要指定的是,如果您打算對位於子文件夾內的腳本使用這些重寫規則,例如

http://site.com/myfolder/account

那么最好在啟動RewriteEngine之后添加RewriteBase

重寫模塊是否啟用? 也許嘗試檢查以斜杠結尾的URL。

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^account\/\?$ /account.php [L]
</IfModule>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM