【问题标题】:After I Change my .htaccess my Css An Js not working在我更改我的 .htaccess 之后,我的 Css An Js 无法正常工作
【发布时间】:2015-01-14 20:06:37
【问题描述】:

我的项目文件夹是这样的

  *myProject(d:wamp/www/mywebsites/myProject)
     @ ajax
     @ asset
        #all of php functions and database
     @ newsPicture
        #my post images
     @ profilePicture
        #user images
     @ public
       -js
       -css
       -img
       -.htaccess //file
       -index.php //file
       -User.php //file
       -Posts.php //file
     @ view
       -header.php  //include in index.php
       -footer.php  //include in index.php

所以我将我的 htaccess 设置为此

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#Change /Posts.php?id=4 => /Posts/4
RewriteRule ^Posts/([A-Za-z0-9]*)$ Posts.php?id=$1 [L]

#Change /User.php?id=4 => /User/4
RewriteRule ^User/([A-Za-z0-9]*)$ User.php?id=$1 [L]

#Change /Sign.php => /Sign
RewriteRule ^([A-Za-z0-9]+)$ $1.php [L]

而我的html头是这样的(view/header.php)

  <link href="css/bootstrap.min.css" type="text/css" rel="stylesheet">
  <link href="css/bootstrap-theme.min.css" type="text/css" rel="stylesheet">
  <link href="css/Normalize.css" type="text/css" rel="stylesheet">
  <link href="css/Fixer.css" type="text/css" rel="stylesheet">

所以当我在我的 url 中传递参数时,比如 User/2,我所有的 css 和 js 都不起作用!我应该在 header.php 中为我的链接标签使用绝对链接吗?喜欢:

<link href="<?php echo dirname($_SERVER['PHP_SELF']);?>/css/Fixer.css" type="text/css" rel="stylesheet">

请帮帮我!

【问题讨论】:

  • 在问这个问题之前,您是否尝试过使用绝对链接?
  • 是的,它有效!实际上我在 中添加了 并且它工作正常,但我想知道有没有其他方法可以在 htaccess 中修复它?或者在我的标题中使用 标记是不是很糟糕?还是我使用绝对链接更好???哪个更安全!

标签: javascript php html css .htaccess


【解决方案1】:

您必须通过在开头添加斜杠来更改路径。

当您访问 www.yourdomain.com/path1/

www.yourdomain.com/path1/css/bootstrap.min.css

中的服务器搜索文件
<link href="/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<link href="/css/bootstrap-theme.min.css" type="text/css" rel="stylesheet">
<link href="/css/Normalize.css" type="text/css" rel="stylesheet">
<link href="/css/Fixer.css" type="text/css" rel="stylesheet">

【讨论】:

  • 来自数据库的用户图片和帖子图片呢?
  • 当用户上传头像时,我将图像保存在数据库中,例如:“../ProfilePicture/HJhbuGj.jpg”,那么应该怎么做呢?!
  • 为什么你用“../”和文件夹名保存名字?如果要更改目录名称会发生​​什么?您只需要保存图像名称。路径将类似于“/path/to/your/profile/images/{$imageName}”
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 2017-11-28
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
相关资源
最近更新 更多