【问题标题】:Convert .php extention to .html for all files in particular folder using htaccess使用 htaccess 将特定文件夹中所有文件的 .php 扩展名转换为 .html
【发布时间】:2011-11-15 01:46:34
【问题描述】:

我有一个名为 Pages 的文件夹,其中包含 .php 文件。我只想将这个特定文件夹的 .php 扩展名转换为 .html。我有 .htaccess 代码来转换扩展名

RewriteRule ^([a-z0-9_]+)\.html$ /index.php$1 [NC,L] 

但我只想对特定文件夹进行更改。

有什么想法吗???

【问题讨论】:

  • 你把 .htaccess 文件放在哪里了?
  • 为什么要在 URL 中显示 .html 扩展名?浏览器不需要它。

标签: php html apache .htaccess


【解决方案1】:

.htaccess 文件与它所在的文件夹以及所有其他子文件夹相关。

因此,如果您只想对一个文件夹应用一些重写规则,则需要在该特定文件夹中放置一个 htaccess。

【讨论】:

  • 我现在已经通过将 htaccess 文件放在特定文件夹中进行检查,但它不起作用。我需要更改代码中的任何内容。
  • 你有时需要添加一个“RewriteBase Pages”
  • 我认为这里不需要 RewriteBase。 @Ullas 您能否发布整个 htaccess 文件并重新检查在所需文件夹上方的目录中是否没有任何其他 htaccess 文件?
【解决方案2】:

使用 .htaccess 文件将子目录中的 .php 扩展名转换为 .html 扩展名(使用 .htaccess 文件将特定文件夹中所有文件的 .php 扩展名转换为 .html 扩展名)

RewriteEngine on  
RewriteBase /subdirectory name

示例

RewriteEngine on  
RewriteBase /gallery

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L]  

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html $1.php [L] 

【讨论】:

    【解决方案3】:

    它将所有 php 文件更改为 html 文件,例如 foo.php 到 foo.html

    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} (.).php
    RewriteRule ^(.*).php $1.html [R=301,L]
    RewriteRule ^(.*).html $1.php [L] 
    

    【讨论】:

      【解决方案4】:

      您可以通过在 .htaccess 中的 RewriteBase 行之后指定文件夹名称来执行此操作,如下所示。

      RewriteEngine On
      
      RewriteBase /sampleFolder/
      
      RewriteCond %{THE_REQUEST} (.).php
      RewriteRule ^(.*).php $1.html [R=301,L]
      RewriteRule ^(.*).html $1.php [L]
      
       
      

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 2019-07-07
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2012-08-02
        • 2015-06-07
        相关资源
        最近更新 更多