CSS,包括&图像VS mod_rewrite,相对路径和基础目录

时间:2014-02-10 14:55:46

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

我想要什么

  • 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和favicon。可能是因为假设它们是/en/style.css

我需要什么

的正确组合
  • 的.htaccess

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

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

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

非常感谢你!

1 个答案:

答案 0 :(得分:0)

首先,您需要了解重写规则无法修复php include,因为这种情况发生在PHP内部,并且控件甚至没有回到PHP。

现在修复css/js/images最佳选择是使用绝对链接。如果出于某种原因,您可以尝试在页面的标题中添加:<base href="/" />

然而,还有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]
相关问题