【问题标题】:handle image URL (.htaccess) by passed variable通过传递的变量处理图像 URL (.htaccess)
【发布时间】:2015-05-28 11:33:16
【问题描述】:

我想以两种不同的方式处理我的图片 URL。 首先,当浏览器调用时: http://example.com/image.jpg(需出示)

其次,当浏览器调用时: http://example.org/image.jpg?actionId=123(需要将URL改写为index.php?actionId=123)

关键是,当 URL 有它指定的变量时,我们需要使用该变量和它的值重定向到 index.php。

我正在使用带有 mod_rewrite 的 .htaccess,现在如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
</IfModule>

所以它检查现有的文件或目录,任何其他方式,它在'q'参数中传递给 index.php。

提前谢谢你!

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    对于第一个示例,您无需执行任何操作,如果图像存在,则会显示该图像。因此您只需要考虑查询字符串的情况:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} actionId=([^&]+)
    RewriteRule \.jpg$ index.php?actionId=%1 [L]
    

    如果您还需要将其他参数传递给index.php,那么只需进行重写就足够了,因为默认情况下会发送查询字符串。确保使用QSA 标志:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} actionId=([^&]+)
    RewriteRule \.jpg$ index.php [QSA,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2012-01-25
      • 2014-08-28
      • 2011-09-25
      • 2016-09-30
      相关资源
      最近更新 更多