Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用htaccess重定向时不显示图像和css_Php_.htaccess_Redirect - Fatal编程技术网

Php 使用htaccess重定向时不显示图像和css

Php 使用htaccess重定向时不显示图像和css,php,.htaccess,redirect,Php,.htaccess,Redirect,当我使用htaccess重定向文件时,图像和css无法正确应用javascript。 例如,我重定向了页面 http://example.com/ 到 为此,我在htaccess中使用了以下代码 RewriteRule ^([a-z]{2})/?$ index.php?cn=$1 现在页面被正确重定向,但css和图像没有重定向。可能是什么问题?我有这样的档案 /index.php /style.css /images/... /.htaccess Edit: <link re

当我使用htaccess重定向文件时,图像和css无法正确应用javascript。
例如,我重定向了页面

http://example.com/  

为此,我在htaccess中使用了以下代码

RewriteRule ^([a-z]{2})/?$ index.php?cn=$1  
现在页面被正确重定向,但css和图像没有重定向。可能是什么问题?我有这样的档案

/index.php
/style.css
/images/...
/.htaccess

Edit: <link rel="stylesheet" type="text/css" href="/style.css" />
/index.php
/style.css
/图像/。。。
/.htaccess
编辑:

您必须在规则之前添加一个
RewriteCond
,以确保实际存在的任何文件(例如-
style.css
)不会传递到
index.php

尝试在现有的
重写规则

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

确保您没有任何绝对的URL问题。使用htaccess时,有时您可能会忘记浏览器认为其访问的URL(您重写的URL)不一定与web根目录中的文件层次结构直接相关。在处理外部内容时,我始终建议使用相对于网站的www根目录的绝对路径。例如,加载style.css文件的正确链接为:

<link type="text/css" rel="stylesheet" href="/style.css" />

而不是

<link type="text/css" rel="stylesheet" href="style.css" />


这样,当浏览器认为它在au子目录中,甚至当它在其他地方时,CSS、JavaScript和图像文件都可以加载,而无需根据重写的URL专门化URL。

显示如何链接到CSS和图像(hrefs)。您的问题中缺少该信息。这是我们可以访问的实时网站吗。在这里,一些代码或问题的实时示例可能会有所帮助。
<link type="text/css" rel="stylesheet" href="style.css" />