【问题标题】:CSS, includes & images VS mod_rewrite, relative Paths and Base DirCSS,包括和图像 VS mod_rewrite,相对路径和基本目录
【发布时间】:2014-02-11 01:08:30
【问题描述】:

我想要什么

  • http://example.com/index.php?lang=en&page=main

    应该看起来像http://example.com/en/main

  • 图像、包含和 CSS 应具有相对路径
  • 网站应该仍然可以通过原始链接访问(如果可能?

我有什么

我已经阅读了很多关于 htaccess、mod_rewrite、BasePath 等的内容。 我尝试了很多组合,但总有一种情况不起作用。

大多数情况下 CSS 未加载(路径错误:/en/style.css)或旧格式的链接不再起作用。

我目前的结构:

/incl
  |incl1.php
  |incl2.php

/img
  |img1.png
  |img2.png

index.php
style.css

我当前的 .htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1&page=$2 [L,NC]

我当前的网站:

<link href="style.css" rel="stylesheet" type="text/css" />
<base href="http://example.com/">
...
<img src="img/img1.png" />
...
<?php include('incl/incl1.php'); ?>

我目前的 CSS:

background-image: url(img/img2.png);

编辑:什么不起作用:

未找到 CSS 和网站图标。可能是因为它们被假定为/en/style.css

我需要什么

正确的组合

  • .htaccess

    • RewriteBase我需要那个吗?我尝试并没有得到更好的结果)
    • RewriteCond
    • RewriteRule
  • 网站

    • &lt;base href="http://example.com/"&gt; &lt;base href="/"&gt; ?
    • src="style.css" src="/style.cs" ?
    • &lt;?php include('incl/incl1.php'); ?&gt; &lt;?php include('/incl/incl1.php'); ?&gt; ?
  • CSS

    • background-image: url(img/img2.png); background-image: url(/img/img2.png); ?
  • 我应该把我所有的图像和 css 放在 /static 子文件夹中吗?我该如何使用它?

非常感谢!

【问题讨论】:

  • 那么到底是什么不起作用?
  • 未找到 CSS 和网站图标。可能是因为它们被假定为/en/style.css。我是否可能只需要在&lt;head&gt; 中的相对路径中添加/ 前缀?刚刚尝试过,它似乎工作,但我仍然需要测试一切。这怎么可能?为什么只在头部?

标签: php css .htaccess mod-rewrite url-rewriting


【解决方案1】:

首先,您需要了解重写规则无法修复 php include,因为这发生在 PHP 内部,并且控制权甚至不会返回到 PHP。

现在修复css/js/images 的最佳选择是使用绝对链接。如果由于某种原因您无法尝试将其添加到页面标题中:&lt;base href="/" /&gt;

不过,mod_rewrie 也可以修复这些规则。

Options +FollowSymLinks
RewriteEngine On

# fix css/js/images link
RewriteRule ^[a-z]{2}/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1&page=$2 [L,NC,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多