【问题标题】:Using if statement in .htaccess file在 .htaccess 文件中使用 if 语句
【发布时间】:2016-02-23 06:10:36
【问题描述】:

使用 .htaccess 文件中的基本正则表达式代码,从站点 url 中删除 .php 扩展名,如下所示:

# Externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

##Internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

这工作正常,但我也有 mail.php 文件,用于发送电子邮件,并且无法将数据发布到该文件,因为来自 .htaccess 的规则,该路径是这样的: www.site/mail 而不是 www.site/mail.php。

那么,有没有办法在 .htaccess 中设置类似:

如果 url 是:www.site/mail.php 不使用规则切割扩展?

【问题讨论】:

    标签: regex .htaccess preg-match


    【解决方案1】:

    实际上,您的第一条规则不应该为任何POST 请求删除.php,否则您将丢失所有POST 数据。您可以使用以下方法修复它:

    # Externally redirect if not POST request
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=302,L,NE]
    
    ##Internally redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多