【问题标题】:.htaccess redirect .php and .html requests.htaccess 重定向 .php 和 .html 请求
【发布时间】:2016-04-03 05:09:51
【问题描述】:

我使用一个名为 SilverStripe 的框架……我们目前正在将一个旧网站迁移到这个框架上。问题是旧站点的 URL 以 .php 或 .html 结尾,而在新站点中却没有。

我需要修改第二个重写规则,以便将请求泵送到 main.php 而不使用任何 .html 或 .php 扩展名。

在我当前的 .htaccess 中,我有以下规则:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
    DirectoryIndex disabled
</IfModule>

SetEnv HTTP_MOD_REWRITE On
RewriteEngine On

# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]

# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]

# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]

可能的解决方案(仍在测试中):

 RewriteCond %{REQUEST_URI} ^(.*)\.html [OR]
 RewriteCond %{REQUEST_URI} ^(.*)\.php [OR]
 RewriteCond %{REQUEST_URI} ^(.*)$
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule .* framework/main.php?url=%1 [QSA]

【问题讨论】:

    标签: .htaccess silverstripe


    【解决方案1】:

    将以下规则添加到# Deny access to potentially sensitive files and folders 规则块下方的.htaccess 文件中:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^(.+)\.html$
    RewriteRule (.*)\.html$ /$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^(.+)\.php$
    RewriteRule (.*)\.php$ /$1 [R=301,L]
    

    前两行检查 url 不是目录也不是文件。

    第三行检查 url 是否包含 .html.php

    第四行从网址中删除.html / .php

    【讨论】:

    • @3dogo 不幸的是规则没有触发
    • 这很奇怪。它适用于我的测试。例如,如果我访问www.example.com/about.html,它将重定向到www.example.com/about。另一个示例www.example.com/about/team.php 将重定向到www.example.com/about/team
    • 你用什么版本的 apache 测试?
    • 这是在 apache 2.2 上。你用的是什么版本的apache?
    • 对我来说,这似乎是最干净的。 @ElGabbu 我希望你注意到浏览器缓存了 301,所以如果你测试了其他一些 301,你应该在尝试新的之前清理你的缓存。
    【解决方案2】:

    您可以稍微调整一下现有规则:

    # Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
    <IfModule mod_dir.c>
        DirectoryIndex disabled
    </IfModule>
    
    SetEnv HTTP_MOD_REWRITE On
    RewriteEngine On
    
    # Enable HTTP Basic authentication workaround for PHP running in CGI mode
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    # Deny access to potentially sensitive files and folders
    RewriteRule ^vendor(/|$) - [F,L,NC]
    RewriteRule silverstripe-cache(/|$) - [F,L,NC]
    RewriteRule composer\.(json|lock) - [F,L,NC]
    
    # Process through SilverStripe if no file with the requested name exists.
    # Pass through the original path as a query parameter, and retain the existing parameters.
    # Strip out .html or .php from request URI
    RewriteCond %{REQUEST_URI} ^(.+?)(?:\.(?:html|php))?$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ framework/main.php?url=%1 [L,QSA]
    
    # If framework isn't in a subdirectory, rewrite to installer
    RewriteCond %{REQUEST_URI} ^(.*)/framework/main\.php$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . %1/install.php? [R,L]
    

    【讨论】:

      【解决方案3】:

      这里有一个简短的解决方案

      试试下面的规则:

      RewriteRule ^([^.]+)\.(html|php)$ /framework/main.php?url=$1 [NC,L,QSA]
      

      这将重写

      /file.php OR /file.html
      

         /framework/main.php?url=$1
      

      模式解释:

      ^([^.]+).(html|php)$ 匹配以除点以外的任何字符开头的任何请求,后跟一个 littrel 点 char,后跟文字 php 或 html URI。

      注意:这应该是您 htaccess 中任何其他规则之前的第一条规则。

      【讨论】:

        【解决方案4】:

        试试这个代码:-

        RewriteEngine on 
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME}\.php -f 
        RewriteRule ^(.*)$ $1.php
        
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME}\.html -f 
        RewriteRule ^(.*)$ $1.html
        

        希望这段代码对你有用:)

        【讨论】:

          【解决方案5】:

          基本的怎么样...

          Redirect 301 /about-us.html http://www.domain.com/about-us
          

          编辑现在你已经提到你有数百个......上面的答案仍然有效,因为你可以将数百个这些添加到 htaccess(我已经看过很多次了) ...但是也很有可能像这样...

          RedirectMatch 301 (.*)\.html$ http://www.domain.com$1/
          

          不逐行执行此操作的缺点是您现在可能有一个特定的 html 文件,您确实希望允许访问它,并且需要将其添加为该规则的例外。

          【讨论】:

          • 因为我有大约 900 个这样的 URL :)
          • 它确实回答了他们的问题,只是碰巧他们现在提供了新信息!
          • 是否有可能以某种方式更改规则... RewriteRule .* framework/main.php?url=%1 [QSA] 这样传递给该 url 参数的内容永远不会有 .html 或.php 扩展名?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-13
          相关资源
          最近更新 更多