Fairly basic mod Rewrite issue

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 03:32:36

问题


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).


回答1:


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?

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]


来源:https://stackoverflow.com/questions/9098418/fairly-basic-mod-rewrite-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!