2014-09-03 131 views
1

我想隱藏PHP擴展,所以我寫了下面的.htaccess文件的代碼,我發現這個鏈接How to remove file extension from website address?自動URL重寫不工作

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

這項工作很好,當我鍵入localhost /測試網站/索引呢正確顯示localhost/testsite/index.php並在地址欄中顯示localhost/testsite/index, ,但是當我強制鍵入localhost/testsite/index.php時,它不會轉換爲localhost/testsite/index。即使用戶在頁面名稱後鍵入.php,我也想刪除擴展名。

+0

恐怕你不能。我擁有超過一般的htaccess文件的經驗,它的工作方式與您的寫作方式相同。你仍然可以訪問完整的路徑,而不是重寫,如果你手動編寫它,我沒有看到任何錯誤! :) – Cowwando 2014-09-03 05:09:16

+0

這是一個重複http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – jbrahy 2014-09-03 05:11:53

+0

在例如stackoverflow站點如果我寫 stackoverflow.com/questions/25636602/automatic-url -rewriting-未working.php?noredirect = 1個#comment40055522_25636602 它將顯示 stackoverflow.com/questions/25636602/automatic-url-rewriting-not-working?noredirect=1#comment40055522_25636602 和將除去PHP擴展。 – 2014-09-03 05:18:44

回答

0

有你的.htaccess這樣的:

DirectoryIndex index.php 
RewriteEngine On 
RewriteBase/

## hide .php extension 
# To externally redirect /dir/file.php to /dir/file 
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC] 
RewriteRule^/%1%2 [R=302,L,NE] 

# To internally forward /dir/file to /dir/file.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]