2016-12-12 17 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

相关问题