Here's the situation. I have directory structure something like this:
root
----admin
------------index.php
----index.php
----images
----stylesheets
----javascripts
THE PROBLEM
All the content of pages are generated by index.php file that loads the content according to given $_GET
variable from the database. I have tinymce in the index.php of the admin folder. So when the content of the pages are shown in tinymce the images and stylesheets etc. looks broken because they are trying to GET something like images/image1.jpg
.
WHAT I WANT
I want to make a .htaccess file to solve this problem ( if possible ). I want to say forexample whenever from admin folder ( Where .htaccess file would be ) comes request like /images
it should consider it as ../images
. Because I don't know how to write .htaccess files that's why I'm seeking help.

- 15
- 4
-
You say "whenever from admin folder ... comes request"; a web request doesn't "come from" any folder, it is just a request from the browser to your server, for a particular URL. If that URL is `/images/image1.jpg`, then that is the only URL you can see in rewrite rules. (There may be a "referer" header in the request, but it's not 100% reliable, and basing an architecture on it is likely to cause more problems than it solves.) – IMSoP Jun 28 '14 at 19:22
-
Oh yeah I mean http://www.domain.com/admin/..... – Faiz Ahmed Rana Jun 28 '14 at 19:23
-
But what do you mean by "comes from"? – IMSoP Jun 28 '14 at 19:23
-
Also, reading again, the URL `/images/image1.jpg` appears to be correct. Do you mean that your HTML contains URLs like `images/image1.jpg` (no leading `/`, so relative to current directory, rather than root of domain), so that the browser is trying to find `/admin/images/image1.jpg`? – IMSoP Jun 28 '14 at 19:25
-
Yeah because `images/image1.jpg` is correct from root but incorrect from admin directory – Faiz Ahmed Rana Jun 28 '14 at 19:27
-
OK, so the leading slashes in your question are incorrect, and adding them to the HTML would in fact solve your problem. However, if that's tricky to do at that stage, it is indeed pretty simple to create rewrite rules for this. – IMSoP Jun 28 '14 at 19:29
-
Yeahh man that's what I am asking please help – Faiz Ahmed Rana Jun 28 '14 at 19:29
-
@IMSoP Please give me rewrite rules – Faiz Ahmed Rana Jun 28 '14 at 19:34
-
1Have some patience, or I will delete the explanation I have half-written. If you want to boss somebody about, pay them. If you want volunteer help, have a bit of humility. – IMSoP Jun 28 '14 at 19:35
2 Answers
From your comments, it seems that what you have are relative URLs like images/image1.jpg
, which the browser will load relative to the current directory, rather than the root of the domain. In general, I would advise you to avoid creating such links whenever possible, by adding a leading /
, but I appreciate it may not be easy to convert if you have lots of existing content.
So to be clear, when HTML is displayed on /admin/index.php
, an image source of images/image1.jpg
will cause a GET request for /admin/images/image1.jpg
. So the simplest (and obviously pretty useless) rule would be this:
RewriteRule admin/images/image1.jpg /images/image1.jpg
Meaning "when you see this URL, actually serve this URL". Note that - somewhat ironically - the left-hand side has no leading /
; that's because inside .htaccess
, rewrite rules are processed relative to the current directory; in a "real" config file (included when Apache starts up) the rule would have a leading /
.
Obviously, you'd wanted to generalise, so you need a really simple regular expression to say "when you see a URL like this, actually serve the equivalent URL like this":
RewriteRule admin/images/(.*) /images/$1
Where (.*)
means match anything of any length, and "capture" it, and $1
means substitute in the first thing captured.
One more thing to consider is whether you want both URLs to simply have the same content (an internal rewrite, which is what the above will do) or whether the browser should be told to load the "correct" URL instead (a redirect), which you can achieve by adding [R=temp]
or [R=permanent]
at the end of the rules. One advantage being that the browser will see that the URLs are the same, and be able to serve the same cached copy.

- 89,526
- 13
- 117
- 169
-
Thanks for the answer but putting this in the root of the project is not working. `RewriteEngine On RewriteRule /admin/images/(.*) /images/$1` – Faiz Ahmed Rana Jun 28 '14 at 19:53
-
Ah, I'm too used to using mod_rewrite in "real" config files (included at Apache startup). I think when using .htaccess you have to omit the leading / (ironic, for this example!). So it would be `RewriteRule admin/images/(.*) /images/$1`. – IMSoP Jun 28 '14 at 19:55
-
Have you remembered to add `RewriteEngine On` at the top? I forgot to mention that, because I saw you'd already heard of mod_rewrite, so I jumped straight into the rules themselves. – IMSoP Jun 28 '14 at 19:57
-
-
-
I don't know what but I messed something up now your solution is working please update your answer for the correction you made in comments. – Faiz Ahmed Rana Jun 28 '14 at 20:23
-
Glad it's working. I've incorporated the correction re .htaccess as you suggest. – IMSoP Jun 28 '14 at 20:43
If your webserver is running on a *nix system, you should be able to use a symbolic link within your admin directory.
ln -s ../images images
You may have to allow the webserver (assuming apache) to follow symbolic links (that can be set in the .htaccess) file.
you may also want to check out: .htaccess config with symbolic links and index files not working as expected