2016-12-12 69 views
0

那麼標題幾乎總結了它。我試圖隱藏的index.php,但任何其他PHP文件,例如file.php將成爲/文件/ etc ...apache mod_rewrite隱藏index.php,但將其餘的php文件更改爲目錄

我有這種htaccess的,到目前爲止,但它不工作...

Options -Indexes -Multiviews 

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/$ $1.php 
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteRule (.*)$ /$1/ [R=301,L] 

任何人都可以幫助我嗎?

謝謝。

回答

0

隱藏index.php的最好方法是永遠不要鏈接到它,這是沒有必要的,我不知道爲什麼人們這樣做。在您的href中使用/鏈接到Web根目錄,或使用./鏈接到當前目錄。

# 404.php file should set status with header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); 
ErrorDocument 404 /404.php 
Options -Indexes -Multiviews 
RewriteEngine On 
RewriteBase/
# remove .php from legitimate requests (you should still update all the URLs served so that normal crawling/browsing doesn't 301 on every link) 
RewriteCond %{THE_REQUEST} ^\S++\s++((/[^?\s]+?)??/index\.php)(\?\S*+)?\s [OR] 
RewriteCond %{THE_REQUEST} ^\S++\s++((/[^?\s]+?)\.php)(\?\S*+)?\s 
RewriteCond %{DOCUMENT_ROOT}%1 -f 
RewriteRule^%2/%3 [NS,NE,L,R=301] 
# add trailing/to legitimate requests missing it 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule .+ $0/ [NS,NE,L,R=301] 
# rewrite all requests that look like directories to files, /foo/ to /foo.php and /bar/baz/qux/ to /bar/baz/qux.php 
RewriteCond %{DOCUMENT_ROOT}$1.php -f 
RewriteRule ^([^./][^/]*+(?:/[^./][^/]*+)*)/$ $1.php [NS,L,DPI] 
# allow everything else to be processed as normal file request or 404 
+0

我想你誤解了我的問題。你寫了「寫所有看起來像目錄到文件的請求」,但我需要將PHP文件更改爲目錄,而不是目錄到PHP文件。另外隱藏index.php我的意思更多的方式,如果用戶鍵入index.php它自動返回只是/和不顯示PHP。 – FoxyFish

+0

因此,PHP文件不再存在,現在的文件夾呢?或者實際的文件就像是'/ bar/baz/qux.php',你希望URL出現*爲'/ bar/baz/qux /'? – Walf

+0

我想你誤解了我的解決方案。你從來沒有說過你想要一個外部重定向,只是你想讓'/ file /'解析爲'file.php',這是通過內部重寫完成的。 – Walf

相關問題