【问题标题】:Proper regex for .htaccess internal redirect and url shortening.htaccess 内部重定向和 url 缩短的正确正则表达式
【发布时间】:2012-09-13 05:58:35
【问题描述】:

基于此线程:Proper regex for .htaccess redirect and prevention of hotlinks

我在问一个更有针对性的问题(盗链已解决)。

我正在做一个图像宿主项目并且正在对图像名称进行编码。我需要为用户提供一个简短的 url 来访问他们的图像。以下内容应在内部重定向:

domain.com/hsh1h.jpg(或其他图片扩展名)
domain.com/hsh1h

这些将在内部重定向到:
domain.com/image.php?query=hsh1h.jpg
domain.com/image.php?query=hsh1h

问题是不应重定向以下网址:

domain.com/admin
domain.com/admin/index.php

我很抱歉之前创建了一个令人困惑的话题,非常感谢大家的帮助!

【问题讨论】:

    标签: regex .htaccess apache2


    【解决方案1】:

    试试:

    # these rules ensure you aren't clobbering legit requests (like to image.php)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # just in case, exclude the admin directory
    RewriteCond %{REQUEST_URI} !^/admin
    
    # rewrite
    RewriteRule ^(.*)$ image.php?query=$1 [L]
    

    【讨论】:

    • 非常感谢您的时间和耐心。我是 stackoverflow 的新手,但您的耐心和知识肯定会让我留在身边。希望我能像您一样为社区做出贡献!感谢您的深入解决方案!
    【解决方案2】:

    应该这样做:

    RewriteCond %{REQUEST_URI} !^/admin
    RewriteRule (.*) image.php?query=$1
    

    但这是列入黑名单 - 一般来说,我建议对此类内容使用白名单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多