如何使用.php重定向请求(扩展名)

时间:2013-12-13 07:24:32

标签: php .htaccess redirect

我找到了部分答案here

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]

[工作] www.example.com/file.php重定向到404

[工作] www.example.com/file在/file.php下投放(不在地址栏上显示)

[工作] www.example.com/folder/不重定向到404

[不起作用] www.example.com/folder/index.php重定向到404

我知道因为DirectoryIndex

,www.example.com/folder/作为www.example.com/folder/index.php提供(没有在地址栏上显示)

我试过玩游戏并禁用DirectoryIndex,但我无法弄明白。

到目前为止我所拥有的:

# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteCond %{REQUEST_URI} !(.*)/index\.php$
RewriteRule ^(.*)$ 404.php [L]

# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

2 个答案:

答案 0 :(得分:1)

试试这个,

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ /yourfolder/index.php?/$1 [L]
</IfModule>

答案 1 :(得分:0)

试试这段代码:

# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} \s.+?\.php[\s?] [NC]
RewriteCond %{REQUEST_URI} !/index\.php [NC]
RewriteRule ^ 404.php [L]

# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]