need help with mod_rewrite (problem with loading resources after rewrite)

need help with mod_rewrite (problem with loading resources after rewrite)

am 05.09.2007 20:11:19 von romayankin

I am rewriting ***http://localhost/home*** and ***http://localhost/
home/*** (notice the forward slash on the end) to ***http://
localhost***

I use the following regexp:
RewriteRule ^home([/]?|[\w./]+)$ /$2


It works perfectly however when the URI contains forward slash on the
end mod_rewrite substitutes wrong paths for all resources on my page
(stylesheets, images, etc)

The following is taken from the log when I query http://localhost/home/
(this is when all resources are substituted with incorrect path) I
have marked with "->" lines where the new rewrite starts

++++++++++++++++++++++++++++++++++++++
-> /initial] (2) init rewrite engine with requested uri /home/
/initial] (1) pass through /home/
/initial] (3) [per-dir E:/www/root/JobGuide/] add path info postfix:
E:/www/root/JobGuide/home -> E:/www/root/JobGuide/home/
/initial] (3) [per-dir E:/www/root/JobGuide/] strip per-dir prefix: E:/
www/root/JobGuide/home/ -> home/
/initial] (3) [per-dir E:/www/root/JobGuide/] applying pattern
'^home([/]?|[\w./]+)$' to uri 'home/'
/initial] (2) [per-dir E:/www/root/JobGuide/] rewrite home/ -> /
-> /initial] (2) init rewrite engine with requested uri /home/css/
generic.css
/initial] (1) pass through /home/css/generic.css
/initial] (3) [per-dir E:/www/root/JobGuide/] add path info postfix:
E:/www/root/JobGuide/home -> E:/www/root/JobGuide/home/css/generic.css
/initial] (3) [per-dir E:/www/root/JobGuide/] strip per-dir prefix: E:/
www/root/JobGuide/home/css/generic.css -> home/css/generic.css
/initial] (3) [per-dir E:/www/root/JobGuide/] applying pattern
'^home([/]?|[\w./]+)$' to uri 'home/css/generic.css'
/initial] (2) [per-dir E:/www/root/JobGuide/] rewrite home/css/
generic.css -> /
/initial] (1) [per-dir E:/www/root/JobGuide/] internal redirect with /
[INTERNAL REDIRECT]
++++++++++++++++++++++++++++++++++++++

Can someone help me with making regexp that would work both with and
without forward slash, I seem to have completely stuck.

Re: need help with mod_rewrite (problem with loading resources after rewrite)

am 06.09.2007 00:13:08 von shimmyshack

On Sep 5, 7:11 pm, Royan wrote:
> I am rewriting ***http://localhost/home***and ***http://localhost/
> home/*** (notice the forward slash on the end) to ***http://
> localhost***
>
> I use the following regexp:
> RewriteRule ^home([/]?|[\w./]+)$ /$2
>
> It works perfectly however when the URI contains forward slash on the
> end mod_rewrite substitutes wrong paths for all resources on my page
> (stylesheets, images, etc)
>
> The following is taken from the log when I queryhttp://localhost/home/
> (this is when all resources are substituted with incorrect path) I
> have marked with "->" lines where the new rewrite starts
>
> ++++++++++++++++++++++++++++++++++++++
> -> /initial] (2) init rewrite engine with requested uri /home/
> /initial] (1) pass through /home/
> /initial] (3) [per-dir E:/www/root/JobGuide/] add path info postfix:
> E:/www/root/JobGuide/home -> E:/www/root/JobGuide/home/
> /initial] (3) [per-dir E:/www/root/JobGuide/] strip per-dir prefix: E:/
> www/root/JobGuide/home/ -> home/
> /initial] (3) [per-dir E:/www/root/JobGuide/] applying pattern
> '^home([/]?|[\w./]+)$' to uri 'home/'
> /initial] (2) [per-dir E:/www/root/JobGuide/] rewrite home/ -> /
> -> /initial] (2) init rewrite engine with requested uri /home/css/
> generic.css
> /initial] (1) pass through /home/css/generic.css
> /initial] (3) [per-dir E:/www/root/JobGuide/] add path info postfix:
> E:/www/root/JobGuide/home -> E:/www/root/JobGuide/home/css/generic.css
> /initial] (3) [per-dir E:/www/root/JobGuide/] strip per-dir prefix: E:/
> www/root/JobGuide/home/css/generic.css -> home/css/generic.css
> /initial] (3) [per-dir E:/www/root/JobGuide/] applying pattern
> '^home([/]?|[\w./]+)$' to uri 'home/css/generic.css'
> /initial] (2) [per-dir E:/www/root/JobGuide/] rewrite home/css/
> generic.css -> /
> /initial] (1) [per-dir E:/www/root/JobGuide/] internal redirect with /
> [INTERNAL REDIRECT]
> ++++++++++++++++++++++++++++++++++++++
>
> Can someone help me with making regexp that would work both with and
> without forward slash, I seem to have completely stuck.



are you really saying your

documentroot is
/var/www/www.example.com/htdocs

and your filesystem looks like this
/var/www/www.example.com/htdocs/home/path/to/html

and you want to put your files in
/var/www/www.example.com/htdocs

so you can write webpages like
http://www.example.com/home/css/generic.css
and have the server serve the physical file
/var/www/www.example.com/htdocs/css/generic.css

RewriteRule ^home/?(.*) /$1 [L]

if so nothing will change in your url or your html code. [L] makes
sure of that.



Or do you mean the other way round, that you want to write

http://www.example.com/css/generic.css
in your web page, and have the server serve from
/var/www/www.example.com/htdocs/home/css/generic.css

RewriteRule ^/?[^home](.*) /home$1 [L]
(untested, it might look nicer with a rewritecond to test for non /
home urls)