【问题标题】:hide .html .php ending over htaccess RewriteEngine隐藏以 htaccess RewriteEngine 结尾的 .html .php
【发布时间】:2022-02-01 14:42:44
【问题描述】:

它确实有效,因为我只有 .html 文件。但是,现在我需要通过表单扩展 .php。

我试图复制 .htaccess 规则,但 htaccess 只采用第一条规则。

我的尝试:

RewriteEngine on
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.html [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.html[? ].*$"
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

有没有办法在同一条规则中隐藏 .php 和 .html 结尾,这样行得通?

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    尝试添加:

    Options +MultiViews
    

    而不是使用mod_rewrite

    或者,你可以试试这个:

    RewriteEngine On
    
    # return 404 if .html or .php is found at the end
    RewriteRule \.(?:php|html)$ - [R=404,L]
    
    # internally rewrite to .php if that exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ((?:^|\/)[^\.]+)$ $1.php [END]
    
    # internally rewrite to .html if that exists
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ((?:^|\/)[^\.]+)$ $1.html [END]
    

    请记住,[END] 会在该规则之后停止处理所有 mod_rewrite 规则,从而停止将重写的 url 重写为 404 的递归。

    或者你可以试试:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} .*\.(?:php|html)[\s\?]{1}
    RewriteRule ^ - [R=404,L]
    
    # internally rewrite to .php if that exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ((?:^|\/)[^\.]+)$ $1.php [L]
    
    # internally rewrite to .html if that exists
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ((?:^|\/)[^\.]+)$ $1.html [L]
    

    第二个不会停止处理所有重写规则。

    【讨论】:

    • 如果我想用 .htaccess 数据上的这个条目打开任何网站,我会收到错误 500。为 Multivew 选项激活 mod_negotiation。
    • @lumpa 嗯。让我检查一下,请稍等
    • @lumpa 立即尝试。我打错字了
    • 我试过了,但现在他没有找到数据(错误 404),但它们仍在服务器上。如果我尝试用 .html 或 .php 打开它也不起作用。
    • @lumpa 立即尝试
    猜你喜欢
    • 2020-05-12
    • 2013-03-13
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2016-11-23
    • 2013-08-13
    相关资源
    最近更新 更多