【问题标题】:How to redirect requests with .php (extension)如何使用 .php 重定向请求(扩展名)
【发布时间】:2013-12-13 07:24:32
【问题描述】:

我找到了部分答案here

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

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

[works] www.example.com/file 在 /file.php 下服务(不显示在地址栏上)

[works] 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]

【问题讨论】:

    标签: php .htaccess redirect


    【解决方案1】:

    试试这个,

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

    【讨论】:

      【解决方案2】:

      试试这个代码:

      # 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]
      

      【讨论】:

      • 这基本上做同样的事情
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2013-09-15
      • 2012-12-20
      • 1970-01-01
      相关资源
      最近更新 更多