htaccess的休息CSS和Javascript当查询字符串传递 [英] .htaccess breaks CSS and Javascript when query-string is passed

查看:164
本文介绍了htaccess的休息CSS和Javascript当查询字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的.htaccess文件有问题。 这是我的地盘结构

I have a problem with my .htaccess file. This is my site structure

/root
   /SubFolder/  # I'm working on this one
      .htaccess
      index.php
      news.php
      /css/
         normalize.css
         main.css
      /js/
         scripts.js
      /admin/
         news.php
         add.php
         edit.php

一切工作正常,只是当我尝试访问以下网址:<?code>编辑/ 2 (这尔德是相同的 edit.php的ID = 2 )的管理​​目录是在子目录

Everything works fine except when I try to access the following url: edit/2 ( this wold be the same as edit.php?id=2 ) in the admin directory which is in the SubDirectory.

这是我的.htaccess文件

This is my .htaccess file

# ##############################################################################
# # URL REWRITES                                                               #
# ##############################################################################
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /SubFolder
    SetEnv WROOT /var/www/vhosts/website.com/httpdocs/SubFolder/
</IfModule>

# ------------------------------------------------------------------------------
# | Custom error messages / pages                                              |
# ------------------------------------------------------------------------------
ErrorDocument 404 /SubFolder/template/404.php

# ------------------------------------------------------------------------------
# | Removing the "www." at the beginning of URLs                  |
# ------------------------------------------------------------------------------
# Uncomment on live website
#<IfModule mod_rewrite.c>
#   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#   RewriteCond %{HTTPS}s ^on(s)|
#   RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
#</IfModule>


# ------------------------------------------------------------------------------
# | Remove the slash                                                           |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/$ $1 [R=301,L]
</IfModule>


# ------------------------------------------------------------------------------
# | Redirect to extensionless url                                              |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ $1 [R=301,L]
</IfModule>


# ------------------------------------------------------------------------------
# | Pretty urls                                                                |
# ------------------------------------------------------------------------------

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^edit/([0-9]+)$ edit.php?id=$1 [L]

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

当我尝试去编辑/ 2 管理​​目录,我的风格,图像和脚本路径都破了。事实上,在铬控制台显示包含路径 http://website.com/SubFolder/admin/css/style.css 而不是 HTTP: //website.com/SubFolder/css/style.css 。如果我去一个正常的页面没有查询字符串,无论是在子目录子目录/管理/ 文件夹,我的风格是正确加载。

When I try to go to edit/2 in the admin directory, my styles, images and scripts paths are all broken. In fact the included path in the chrome console shows http://website.com/SubFolder/admin/css/style.css instead of http://website.com/SubFolder/css/style.css. If i go to a normal page without query-strings, in either the SubDirectory or the SubDirectory/admin/ folder, my styles are loaded correctly.

为了记录,这是我包括&LT;链接&GT; &LT;脚本&GT; 管理​​目录:&LT;链接相对=样式表的href =../ CSS / normalize.css&GT;

Just for the record, this is how I include <link> and <script> in the admin directory: <link rel="stylesheet" href="../css/normalize.css">

我试图改变的RewriteBase 只是 / /子文件夹/管理员但我所有的页面已经打破路径。

I tried to change the RewriteBase to just / and /SubFolder/admin but then all of my pages have broken paths.

我也尝试添加&LT;基地的HREF =/管理/&GT; ,但仍然打破路径

I also tried to add <base href="/admin/"> but still broken paths.

我怎样才能解决这个问题的任何建议或帮助?

Any suggestions or help of how can I fix this ?

感谢你在前进。

推荐答案

您可以请尝试2种方式来解决这个问题:

You can try one the 2 ways to fix this:

  1. 使用绝对路径在你的CSS,JS,图像文件,而不是相对的。这意味着你必须确保路径的这些文件开始无论是与的http:// 或斜线 / 。所以,你的CSS链接应该是这样的,例如:

  1. Use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /. So your css links should be like this for example:

<link rel="stylesheet" href="/SubFolder/admin/css/style.css">

  • 如果切换到绝对链接是不是一种选择,那么请尝试将这一规则为您的第一个规则在当前的.htaccess:

  • If switching to absolute links is not an option then try inserting this rule as your very first rule in your current .htaccess:

    RewriteRule /((?:css|js|images)/.+)$ $1 [L,NC,R=301]
    

  • 这篇关于htaccess的休息CSS和Javascript当查询字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆