的.htaccess - 重写URL请求并利用css文件护理 [英] .htaccess - rewriting url requests and taking care of css files

查看:123
本文介绍了的.htaccess - 重写URL请求并利用css文件护理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关使用htaccess的重写的URI,也照顾到了浏览器的请求所包含的文件(如.css文件)的事实,一个小问题是错误的。

A little question about using htaccess to rewrite uri's and also take care of the fact that browsers requests for included files (like .css files) are "wrong".

首先,背景:我已经在服务器上的文件结构:

First of all, the background: i have this file structure on the server:

/mysite/.htaccess
/mysite/articles.php
/mysite/includes/css-files/layout.css

和成 articles.php 我行

<style type="text/css">
  @import url(./includes/css-files/layout.css);
</style>

一切都应该很好地工作,当我直接要求PHP文件。但是,因为我们不希望直接提出要求,但使用重写规则,使网址用户/ CEO友好,我们碰上了相对路径的问题。因此,举例来说,有这些行到.htaccess文件

everything should work well, when i directly request the php file. But, since we don't want to request it directly, but using the rewrite rules to make the url user/ceo-friendly, we run into a problem with relative paths. So, for example, having these lines into the .htaccess file

# take care of different requests
RewriteRule ^articles/writer/(\w*)/?$ articles.php?writer=$1
RewriteRule ^articles/tag/(\w*)/?$ articles.php?tag=$1

当用户进入的mysite /文章/作家/ erenor 时,服务器将请求重定向到的mysite / articles.php?作家= erenor 。这是好事。然后,浏览器尝试请求css文件,但该请求将以下内容:

when the user goes to mysite/articles/writer/erenor, the server redirects the request to mysite/articles.php?writer=erenor. This is good. Then, the browser tries to request the css file, but the request will be the following:

GET mysite/articles/writer/includes/css-files/layout.css

这是不好的。所以,我尝试添加这样的规则:

And this is not good. So, i tried to add a rule like this:

# take care of adapting "base url" for css files
RewriteRule /(\w*)\.css includes/css-files/$1.css

有似乎工作,并且该文件被从正确的位置请求的

It seems to work, and the file is requested from the correct location.

现在,这个问题(S):这是一个正确的做法?我能承担的安全问题?有没有更好的东西在程序员的世界吗?

Now, the question(s): is this a correct approach? Could i incur in security issues? Is there something better out in the programmer's world?

感谢您的支持。

推荐答案

该方法可能是不太可取的,因为你现在有2个URL的指向相同的资源。东西,你可以做的是重定向,而不​​是改写:

The approach is probably less desirable as you now have 2 URL's that point to the same resource. Something that you can do is redirect instead of rewrite:

RewriteRule /(\w*)\.css includes/css-files/$1.css [L,R=301]

但你的问题的来源是这样的:

But the source of your problem is this:

@import url(./includes/css-files/layout.css);

有一个中的前/包括这使它成为一个相对URL,浏览器有决定当它要求layout.css中的URI基地使用的。至于它知道,它请求的资源是 / mysite的/文章/作家/ erenor ,所以很明显它要承担的基地 / mysite的/文章/作家/ 。它不知道你重写任何事情,它做的事情的正确方法。为了避免这种情况,要么改变你的网址是绝对的:

There's a . in front of the /includes which makes it a relative URL, and the browser has to determine what the URI base to use when it goes to request layout.css. As far as it knows, the resource it requested is at /mysite/articles/writer/erenor, so obviously it's going to assume the base is /mysite/articles/writer/. It doesn't know anything about your rewrites and it's doing things the correct way. To avoid this, either change your URLs to be absolute:

@import url(/mysite/includes/css-files/layout.css);

或正确的相对URI基地添加到页面的页眉:

or add the correct relative URI base to the page's header:

<base href="/mysite/" />

这篇关于的.htaccess - 重写URL请求并利用css文件护理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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