【问题标题】:htaccess how to delete multiple extensionshtaccess如何删除多个扩展名
【发布时间】:2017-09-30 17:46:19
【问题描述】:

我想从我的 URL 中删除 2 个扩展名(png、gif) 我的代码看起来像这样,但只有第一种类型的扩展有效,例如当 png 是第一次时,扩展名被删除但你无法打开 gif 图像,当我将 png 更改为 gif 时,我可以打开没有扩展名的 gif 但我无法打开 png 图像。

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.png [NC,L]
RewriteRule ^([^\.]+)$ $1.gif [NC,L]






RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+\.png)$ /imgs/$1 [L,NC]
RewriteRule ^([^./]+\.gif)$ /imgs/$1 [L,NC]

我的 .htaccess 在主目录中,

只是主目录> imgs

在“imgs”文件夹中,您只能找到 .png 和 .gif 文件。

【问题讨论】:

  • 您正在访问,例如。 localhost/imgs/23252768.giflocalhost/imgs/23252768localhost/23252768.gif?
  • 我正在尝试从 localhost/23252768 提供它但是当我使用这种方式时,我只能对 gif 图像执行此操作。对于 png 我需要在末尾添加 .png
  • 我在下面更新了我的回复。请尝试使用新的 sn-p。
  • 它正在工作!非常感谢。
  • 很高兴知道! :) 另外,请将答案标记为已选择,这样它就不会再显示为未回答的问题了。

标签: .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

当您在内部将 URL 写回以具有 .png.gif 扩展名时,您还需要检查它们是否存在,否则,像 /path/to/image.gif 这样的路径将被重定向到 /path/to/image,并且将总是重写为不存在的/path/to/image.png

试试下面的规则集:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/imgs%{REQUEST_URI}.png -f
RewriteRule ^([^\.]+)$ $1.png [NC,L]

RewriteCond %{DOCUMENT_ROOT}/imgs%{REQUEST_URI}.gif -f
RewriteRule ^([^\.]+)$ $1.gif [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)\.(gif|png)$ /imgs/$1.$2 [L,NC]

【讨论】:

  • 使用该代码 gif 图像正在工作,但 png 不是服务器响应 png 图像“在此服务器上找不到请求的 URL /imgs/23252768.gif。”看起来它正在搜索 gif 而不是 png。
  • @adamadamowki 您能否提供您的 htaccess 文件在哪里,服务器目录结构是什么以及您正在访问哪个文件?请在您的问题中进行相同的编辑。
【解决方案2】:

试试这个代码:

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(.*?)\.(png|gif)[\s?/] [NC]
RewriteRule  (.*)$  /%1%2 [R=302,L,NE]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2015-10-23
    • 2011-04-30
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多