【问题标题】:Mod rewrite condition for subdomain with pass through具有传递的子域的Mod重写条件
【发布时间】:2016-11-07 05:18:16
【问题描述】:

我想由你运行它,因为我正在尝试完善一个 Apache mod 重写规则,它有效,但我要疯了,因为我的文件路径坏了。

基本上,我已经设置了一个 mod 重写规则来将子域重定向到我的 docroot 中包含静态 index.html 文件的子目录。现有规则如下:

  RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
  RewriteCond %{REQUEST_URI} !^/subdirectory/
  RewriteRule ^(.*)$ /subdirectory/$1 [PT]

重定向工作正常。但会发生什么,是我的图像正在破坏。例如,带有以下 img 标签的 '/subdirectory/img' 对我不起作用:

   <img src="img/some-image.png" />

当我将图像移至根目录并尝试直接调用图像减去“img/”路径时,也会发生同样的情况。

我的重定向与此有关吗?还是我错过了什么?我的印象是,使用 Pass Through [PT] 标志意味着资产链接会起作用。

非常感谢任何建议。感谢您的帮助!

标记。

【问题讨论】:

    标签: apache .htaccess mod-rewrite drupal acquia


    【解决方案1】:

    这是因为您为图片使用了相对 url 路径。要么使用以 / 开头的绝对路径,要么在网页的 head 部分添加以下基本标记:

    <base href="/">
    

    相关: Seo Friendly Url css img js not working

    【讨论】:

    • 恐怕没有这么简单。虽然我希望是。无论我使用什么路径,图像都会导致 500 服务器错误。我认为它与 .htaccess 文件有关。未找到 subdomain.mydomain.com/img/some-image.png。
    【解决方案2】:

    问题已解决。该规则完美运行,它只是 .htaccess 文件中的定位。我把它直接移到了下面:

      RewriteEngine     On
    

    图像和 JS 资源现在可以正常加载。我还添加了一个缺失的“L”标志。

    最终工作代码:

      RewriteEngine     On
      RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
      RewriteCond %{REQUEST_URI} !^/subdirectory/
      RewriteRule ^(.*)$ /subdirectory/$1 [PT, L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 2014-06-30
      • 2012-11-20
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2012-09-22
      相关资源
      最近更新 更多