.htaccess Rewrite rule HELP!

Hi ,
I need to do a url rewrite such that files from the physical location parent/app/webroot/js/abc.js can be accessed by the url parent/js/abc.js
What Should be my rule.
Thanks in Advance


Please post mod-rewrite code that you have tried. We try not to do your work for you. It involves reading about mod-rewrite and serching on google for tutorials or reading over the documentation on apache.org.

I am using these at the moment
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]


rewrite module is already on, but it doesn’t seem to work :S




[standard rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]

[standard rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).

No, I’m not calling you an idiot, I’m saying not to abuse your server like this![/indent][/standard rant #4]

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too. One of the first things to learn about mod_rewrite is when NOT to use it. IMHO, you should change your DocumentRoot to webroot but, if that’s not possible, use a Redirect statement as that’s what it’s designed to do.




[standard rant #1 response]

Within dklynn’s explosive opinion is at least one good point: that (.) needs to be used carefully. But beyond that bit of caution, don’t be dissuaded. The use of (.) is standard practice, and it’s frequently used even in the Apache documentation itself. If you do read dklynn’s tutorial, I strongly suggest you treat it as only a secondary supplement, and instead learn primarily from the official Apache documentation.

[/standard rant #1 response]

With that out of the way, back to your question.

Based on your current htaccess and folder structure, I suspect you’re using CakePHP. If that’s the case, then you should already be getting your desired behavior, because CakePHP provides an [url=https://github.com/cakephp/cakephp/blob/master/.htaccess]htaccess for the “parent” folder that will already rewrite everything to app/webroot. If for some reason that htaccess is missing, then you can replace it with the copy I just linked to.

Regarding my Problem:
@ Jeff Mott

Thank you very much for your help , actually this was precisely what my problem was…I am using CakePHP and the htaccess in the parent folder was missing …Thanks again.

The discussion is getting very interesting and now I am a bit confused that when redirection is there to do the same job as url rewriting , why is it being used so widely , what are the advantages of using url rewriting over redirection ? what can we do with url- rewriting that we cannot do with redirection?
can anyone provide me with that Insight?!!

Thank you All for your comments…they are extremely valuable…

The difference can be thought of as external vs internal. Redirection means sending a 3xx status back to the browser with a new URL. Rewriting means you’re changing how the server maps a URL path to a filesystem path. Redirection is useful if you want the outside world to be aware of the change, such as to let search engines know that a resource has moved. Rewriting is useful if you want the change to be transparent to the outside world, such as sending all requests to index.php.

There are a few differences:

  1. mod_alias is a core module, i.e., it is enabled by default and will always be run before mod_rewrite directives.
  2. mod_rewrite (RewriteRule et al) uses regular expressions while most of the mod_alias (Redirect) directives do not which translates directly to speed (mod_alias is faster).
  3. The POWER of regular expressions (especially when using RewriteCond statements to examine other variables) make it a more versatile tool.

The first thing to learn before you use mod_rewrite, even before proper regex, is when to use it and when not to use it.



Thanks for the comments everyone, I get it now.