【问题标题】:Rewrite an URL and also block the file extension?重写 URL 并阻止文件扩展名?
【发布时间】:2010-01-28 06:31:15
【问题描述】:

是否可以重写 url 并阻止文件扩展名?

例如:如果我有

`http://www.example.com/search.php?...`

我可以改写成

`http://www.example.com/search?` ?

但是,阻止对 .php 文件的访问,通常我可以通过两种方式访问​​,有和没有文件扩展名,但如果我使用 search.php 访问,我希望它显示错误页面或 404。

有可能吗?

谢谢。

【问题讨论】:

    标签: apache url url-rewriting


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} .*\.php
    RewriteRule ^(.*)$ error404.html [R=404]
    

    【讨论】:

    • 我可以用 .php 访问,但没有它就不行。
    • 这不是规则所说的。你确定你正确地应用了它们吗?
    • 好的,现在它可以工作了,但不太好,它完全阻止了文件。有和没有扩展名。
    【解决方案2】:

    请考虑使用以下配置:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      相关资源
      最近更新 更多