相当基本的 mod 重写问题
我的网站通过 index.php 脚本过滤所有内容,在阅读 mod_rewrite 的文档时,这似乎非常简单,但我有点卡住了。我想我应该一次开始一页,看看是否可以让它工作,但很快就陷入了困境。
我有用户配置文件,其长格式基本上是:
http://www.mysite.com/index.php?content=profile&id=2172
因此,我向位于根文件夹中的 .htaccess 文件添加了一个重写规则:
RewriteRule ^profile/([0-9]+)$ index.php?content=profile&id=$1
现在的想法是能够输入 mysite.com/profile/2172< /code>
重定向确实会显示正确的页面,但发生的情况是每个 CSS 文件、图像等都在中间添加了 /profile/,这当然不是图像和 CSS 文件所在的位置。我在代码中使用相对路径名,因此代码中的图像示例可能是: images/userimage.jpg
发生的情况是上面显示的相对链接变成了:
mysite.com/profile/images/username.jpg
对我来说,这使得没有由于图像路径与重写模式 (/profile/*
) 不匹配,那么为什么虚假路径会将 /profile/
添加到我的所有内部链接中?
我尝试在重写规则之前将 RewriteCond %{SCRIPT_FILENAME} !-f
添加到 htaccess 只是为了看看发生了什么,没有任何变化。
抱歉,如果这是一个简单而基本的问题,但是任何具有真正的 mod 重写专业知识的人都可以给我一些指示,以便我可以使这个简单的案例发挥作用,并在我的代码中对我包含的 css 和图像文件提供正确的引用来打开页面?我没有对重写规则使用任何标志,因为在引擎打开后我只有这一行(并且 followsymlink 行也在那里)。
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
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
The idea is now to be able to enter mysite.com/profile/2172
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
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?
I tried adding RewriteCond %{SCRIPT_FILENAME} !-f
to the htaccess just before the rewriterule just to see what happened, no change.
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的图像与所提供的页面相关,该页面确实有
/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.