基本相当的mod重写问题 [英] Fairly basic mod Rewrite issue

查看:43
本文介绍了基本相当的mod重写问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站通过index.php脚本过滤了所有内容,并且在阅读mod_rewrite的文档时,看起来似乎很简单,但是我有点卡住了.我想我可以一次开始一页,看看是否可以使它正常工作,并且很快就卡住了.

My site filters everything through the index.php script, and in reading the docs for mod_rewrite, it seems pretty darn easy, but I'm a bit stuck. I figured I would begin a page at a time to see if i could get it to work, and got stuck pretty quickly.

我有用户个人资料,其基本格式基本上是:

I have user profiles, the longform of which is basically:

http://www.mysite.com/index.php?content=profile&id=2172

因此,我向位于根文件夹中的.htaccess文件添加了一个重写规则:

So i added one Rewrite rule to my .htaccess file that sits in the root folder:

RewriteRule ^profile/([0-9]+)$ index.php?content=profile&id=$1

现在的想法是能够输入mysite.com/profile/2172

The idea is now to be able to enter mysite.com/profile/2172

重定向确实会显示正确的页面,但是发生的是每个CSS文件,图像等都在中间添加了/profile/,这当然不是图像和CSS文件所在的位置.我在代码中使用了相对路径名,因此代码中的图像示例可能是:images/userimage.jpg

The redirect does bring up the proper page, but what is happening is that every CSS file, image, etc is getting /profile/ added in the middle, which is of course not where the image and CSS files are located. I use relative pathnames in the code so an example of an image in the code might be: images/userimage.jpg

正在发生的事情是,上面显示的相对链接变成了:

What is happening is that the relative link shown above gets turned into:

mysite.com/profile/images/username.jpg

对我来说这是没有意义的,因为图像路径与重写模式(/profile/*)不匹配,那么为什么虚假路径将/profile/添加到我的所有内部链接中?

To me that makes no sense as the image path does not match the rewrite pattern (/profile/*), so why does the bogus path add /profile/ to all of my internal links?

我试图在重写器之前将RewriteCond %{SCRIPT_FILENAME} !-f添加到htaccess中,只是想看看发生了什么,没有任何变化.

I tried adding RewriteCond %{SCRIPT_FILENAME} !-f to the htaccess just before the rewriterule just to see what happened, no change.

很抱歉,如果这是一个简单而基本的问题,但是具有真正的mod重写专业知识的任何人都可以给我一些指导,以便我可以完成此简单案例并在我的代码中显示对包含的CSS的正确引用的页面.和图像文件?我没有对Rewriterule使用任何标志,因为在引擎启动后我只有那一行(而且还有ymsymlink行).

Sorry if this is a simple and basic question, but can anyone with real mod rewrite expertise give me some pointers so that I can make this simple case work and bring up the page with the proper references in my code to my included css and image files? I didn't use any flags to the Rewriterule since I only have that one line after the engine is turned on (and the followsymlink line is there as well).

推荐答案

对我来说这是没有意义的,因为图像路径与重写模式(/profile/*)不匹配,那么为什么虚假路径将/profile/添加到我的所有内部链接中?

To me that makes no sense as the image path does not match the rewrite pattern (/profile/*), so why does the bogus path add /profile/ to all of my internal links?

您的图片是相对于所投放页面的,该页面确实具有/profile/例如mysite.com/profile/2172.您可能要考虑将图像/css链接更改为从根目录开始的绝对路径,即/images/imag.jpg.

Your images are relative to the page being served, which does have /profile/ e.g. mysite.com/profile/2172. You may want to consider changing your image/css links to be absolute from root i.e. /images/imag.jpg.

如果不可能,则可以使用以下重写来更正此问题(仅在这种情况下,一般不建议这样做),如下所示.

If that is not possible you could use another rewrite to correct the issue (just for this case, not really recommended in general) as below.

RewriteEngine on
RewriteBase /

#rewrite to remove profile for jpg, css etc, must go above existing rule below
RewriteRule ^profile/(.+\.(jpg|css|js|png))$ $1 [L,NC]

#existing rule
RewriteRule ^profile/([0-9]+)$ index.php?content=profile&id=$1 [L,NC]

这篇关于基本相当的mod重写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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