Mod_rewrite problem

Ok, here’s what I want:

I have a site on a domain, say, mysite-dot-com

I also have a local copy locally under localhost/mysite

I want to rewrite all urls in form mysite-dot-com/param1/param2/param3/…
–> mysite-dot-com/index.php?q=param1/param2/param3/…

However, I want image (and some other files’) paths to remain intact, so src=“images/pic1.jpg” is not rewritten…

I tried putting this htaccess into site’s folder:

RewriteEngine on

RewriteRule ^([a-zA-Z0-9_\\/\\-]*)(\\.){1}(jpg|png|gif|css|js|ico|swf|html){1}$ $1.$3 [L]
RewriteRule ^([a-zA-Z0-9_\\/,\\-:]*)$ index.php?q=$1 [L]


I have to prefix paths of images with site’s base url (eg img src=“/images/pic1.jpg”) in order to show them. That works fine on server
if the site is on localhost, it’ll look for images on localhost/images… instead on localhost/mysite/images

If I put just img src=“images/pic1.jpg” it will look for siteurl/param1/param2/…/images/pic1.jpg

Changing image paths in code is out of question (because most of the images are uploaded through in-site content editor, and I don’t want to be parsing that content)

Is there any solution to the problem?

Even if it requires diff. htaccess for online and local.


First, WELCOME to SitePoint’s Apache forum.

Second, please learn to look at a few threads and/or use the search function as you’ll quickly find the answer(s) you’re looking for.

Third, [begin rant] you’re going about the task of referencing three parameters all wrong. A quick read (if that’s possible) of the mod_rewrite tutorial linked in my signature would steer you in the right direction (if not give you usable code) for most tasks that members have asked about. [end rant]

If you want to exclude existing files from your redirection, why not just say so? mod_rewrite has a very nice one liner to prevent redirection of existing files (okay, two lines if you include directories).

# if the request is NOT a file
RewriteCond %{REQUEST_FILENAME} !-f
# if the request is NOT a directory
RewriteCond %{REQUEST_FILENAME} !-d

That’s the basic structure of WordPress’s mod_rewrite with EVERYTHING else being redirected at index.php which then examines $_SERVER[‘THE_REQUEST’] for all necessary parameters). All you’d need to append to those RewriteCond statements is:

RewriteRule ^([a-z]+)/([0-9]+)/([A-Z]+)$ index.php?a=$1&b=$2&c=$3 [L]

Of course, I’ve specified that your param1 is a string of one or more lowercase letters, param2 is a string of one or more digits and param3 is a string of one or more UPPERCASE letters.

Fourth, you’re really asking about using a VirtualHost on your test server to mimic the online (production) server. Look at your httpd-vhosts.conf file for the format of the necessary VirtualHost settings (or come back and ask here) AND add mysite

to your hosts file (Windows/system32/drivers/etc/hosts - it’s a text file with no extension). Of course, the “mysite” (I prefer to use tld-less domain names locally) must match the httpd-vhosts.conf definition (of ServerAlias).

How easy is that?



Thanks for the answer and for the welcome, in fact I did search and found, well, the same thing you told me, but I was confusing the rewrite problem with vhosts, so I was stuck running in circles with no solution… I was looking for virtual host, just didn’t know that’s what I was looking for.
Hopefully this will solve my problems. And silly me for not looking on how someone else solved this - but you know, I banged my head all day about this, and even NetBeans started failing at autocomplete (!), so the only thing I could do is to cry on forum (apart from eating my keyboard, and/or smashing the monitor…)


If your computer (and monitor) survived the onslaught, please let me know whether you’ve succeeded or not. I’ll post some of my VirtualHost (minimal code for my test server) and I will get you where you need to be.



Yeah, it survived, and of course now everything works as it should. Next time, I’ll try to be calm (and not try to resolve crucial things hours before the deployment) :wink:


No problems at all! I’ve been there and done that, too. Actually, I have to admit to solving many a coding problem after giving up and taking a shower before going to bed for the night. It’s difficult to access the computer from the shower - but a “push back” and calmer state of mind does work wonders!

'Glad you’ve got it resolved.