我使用php來構建web應用程序,但是我希望我的網頁在瀏覽器地址欄中沒有.php擴展名。例如http://www.example.com/index.php
在瀏覽器的地址欄中顯示如http://www.example.com/index
。如何從地址欄中隱藏.php
我怎樣才能做到這一點?
我使用php來構建web應用程序,但是我希望我的網頁在瀏覽器地址欄中沒有.php擴展名。例如http://www.example.com/index.php
在瀏覽器的地址欄中顯示如http://www.example.com/index
。如何從地址欄中隱藏.php
我怎樣才能做到這一點?
在你的WWW根命名.htaccess
文件將這個:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
釷如果你正在運行Apache並且啓動了mod_rewrite,那麼這些工作是有效的。
你能解釋一下嗎? – 2014-02-21 06:54:05
@ ling.s:現在就看到你的評論。當然。這裏重要的是RewriteRule,它將任何請求URI的完整路徑與任何PHP文件進行匹配,並將其傳遞給文件$ 1.php,$ 1是該正則表達式的第一個(也是唯一)匹配項。 – 2015-11-26 10:12:13
在使用Apache網絡服務器的系統上,您可以使用mod_rewrite。
在較新版本的IIS上,您可以使用它們的mod_rewrite版本。在舊版本中,你需要購買一個插件。
搜索棧溢出,你應該找到答案,已經提出的問題。
只是要指出,在IIS的舊版本,例如IIS6,並假設你是在一個32位的進程,然後IONICS ISAPI重寫是一個夢幻般的免費網址重寫模塊。在IIS 6的64位內部,我發現商業產品Helicon ISAPI Rewrite 3是一個很好的工具。但是,如果你使用32位,IONICS是免費的,你可以做任何你需要的。
在apache2.conf我
<Files data>
ForceType application/x-httpd-php
</Files>
這意味着data
被視爲一個PHP文件不帶擴展名
有這樣做的幾種方法。
您可以使用mod-rewrite將foo重新連接到foo.php,這樣/ bar的請求就會被/bar.php處理。
您可以使用目錄和默認的文件,讓你鏈接到豬病/富/它獲取由/foo/index.php
處理您可以設置一個PHP腳本作爲處理程序404 - 錯誤,那麼你只需鏈接到不存在的文件,然後處理程序文件處理它,但它喜歡。 (通常通過使用從url到php文件的某種映射)
您可以告訴您的網絡服務器,某個網絡服務器的所有請求都由php來處理。
第一個或第二個解決方案是最簡單的,但最後的兩個解決方案提供了最好的靈活性,其變體是大多數大型框架所做的。
這是用於URL重寫的a beginners tutorial。
在WAMP只需創建一個.htaccess文件/ WWW/和複製粘貼此代碼..
Options +FollowSymlinks
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
希望!這對某人會有用!
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase/
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule^%1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
little modification - better to use REQUEST_URI and check for the directory
# Turn mod_rewrite on
RewriteEngine on
# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule^%1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule^%1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
歡迎來到StackOverflow!上面的代碼究竟在哪裏?請簡要說明所發佈的任何代碼的上下文。 – 1000Nettles 2018-03-03 03:29:01
你必須使用htaccess的爲 – 2011-04-29 11:58:15
某種類型相關:[?爲什麼有些網站的網址不包括文件擴展名(HTTP:// stackoverflow.com/questions/3631153/how-come-some-site-urls-do-not-include-a-file-extension) – Gumbo 2011-04-29 12:04:20