【问题标题】:Remove file extension php except for one file删除文件扩展名 php 除了一个文件
【发布时间】:2016-07-01 16:19:13
【问题描述】:

我的 htaccess 文件完美地从所有 php 文件中删除了所有文件扩展名,这很好,但是我需要将文件/页面 booking.php 从其中排除并加载 .php 扩展名。实现这一目标的最佳方法是什么?我在 Ubuntu 14.04 上使用 Apache 2.4.7

我当前的 htaccess 文件:

    <IfModule mod_rewrite.c>

     Options +FollowSymLinks -MultiViews -indexes
     RewriteEngine On
     RewriteBase /

     RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
     RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

     RewriteCond %{THE_REQUEST} \s/+sections/(.+?)(?:\.php)?[/?\s] [NC]
     RewriteRule ^ %1 [R=301,L]

     # remove index
     RewriteCond %{THE_REQUEST} /index(\.php)?[\s?/] [NC]
     RewriteRule ^(.*?)index(/|$) /$1 [L,R=301,NC,NE]

     # remove .php; use THE_REQUEST to prevent infinite loops
     RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
     RewriteRule (.*)\.php$ $1 [L,R=301]

     # remove index
     RewriteRule (.*)/index$ $1/ [R=302]
     # remove slash if not directory
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} /$
     RewriteRule (.*)/ $1 [R=301,L]

     # add .php to access file, but don't redirect
     RewriteCond %{REQUEST_FILENAME}.php -f
     RewriteCond %{REQUEST_URI} !/$
     RewriteRule (.*) $1.php [L]

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule (?!^sections/)^(.+)$ /sections/$1 [L,NC]

    </IfModule>

我尝试了一些方法,但没有任何结果,因为总是导致 404 并且从不保留文件扩展名。一些专家的帮助将不胜感激

【问题讨论】:

    标签: regex apache .htaccess ubuntu


    【解决方案1】:

    将以下规则放在顶部(下面RewriteEngine

    RewriteRule ^filebooking\.php$ - [L]
    

    filebooking.php 将通过 - 目标原封不动地传递。

    【讨论】:

    • 非常感谢。我已经尝试过你的方法,但我仍然收到 404 错误。文件 booking.php 在 https 上运行是否可能与此有关?
    【解决方案2】:

    根据您的需要,您可以通过在开头退出规则链将filebooking.php 从任何重写中完全排除

    RewriteRule ^booking\.php$ - [L]
    

    RewriteRule

    -(破折号)
    破折号表示不应执行替换(现有路径通过未触及)。当需要在不更改路径的情况下应用标志(见下文)时使用此选项。


    如果不是每个规则,另一种方法是在每个相关规则前加上 RewriteCond 前缀并排除 filebooking.php

    RewriteCond %{REQUEST_FILENAME} !booking\.php
    

    【讨论】:

    • 非常感谢您的回答。我已经尝试了您的方法,但仍然收到 404 错误,并且文件扩展名似乎没有被删除。文件 booking.php 在 https 上运行是否可能与此有关?
    • 也许我误解了你的问题,但我以为你想从重写中排除booking.php
    • 加密在这里不重要,因为你没有条件检查这个。
    • 我错误地写了filebooking.php 而不是booking.php,也许是这样?
    【解决方案3】:

    在这个SO-post 中找到了一种可能的方法来解决这个问题:

    RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
    

    在上面的例子中,用户想要排除他的 index.php。上面链接的最佳答案也显示了不同的方法。只需尝试将其替换为您的特定文件名即可。

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2017-04-22
    • 2015-10-16
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多