hello im a little new to htaccess specially mod_rewrite, i know mod_rewrite allows us to rewrite request. Can anyone write me the exact code how to rewrite my path to files?. Before my files(jc,css,images..etc.) reside in example /images/image.png, but now it has a parent directory folder named "public" so my path now is /public/images/image.png. how can i rewrite it? i hope some will help me so i don't have to do the hard way and add all paths to /public.

3 Years
Discussion Span
Last Post by cereal

So, in your pages you have:


But you need to change everything to:


And the same goes for css and js, correct? In this case you don't need mod_rewrite because this won't redirect the request to the correct path, you need mod_alias. There are different directives that you can use, for example Alias that rule works like this:

Alias /url/segment /directory/path

So, in your case you would use this setup:

Alias /images /public/images
Alias /css /public/css
Alias /js /public/js

Whenever you access to http://localhost/images/image.png the server will search the file inside /public/images/.

This solution does not work in .htaccess context, you can apply it at server config or virtual host config.

Otherwise you can use the Redirect directive, this can be applied in .htaccess context:

Redirect /images /public/images
Redirect /css /public/css
Redirect /js /public/js

As you see, the first parameter is the url segment, the second the real path. There are also RedirectMatch and RedirectPermanent, this last is used to send a 301 header status, that will inform spiders and browsers that the redirect is permanent.

The difference between Alias and Redirect directives is that the former will be transparent to the client, he won't see the real path, the latters instead will redirect the requests to the real path.

Documentation: http://httpd.apache.org/docs/2.2/mod/mod_alias.html

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.