You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Denis Gerasimov <de...@vekos.ru> on 2005/03/30 07:58:36 UTC

[users@httpd] Rewriting all URLs to /index.php

Hello list,

I need to rewrite all requested URLs for .html files in order to process
them with one script, index.php (I use PHP).

I put the following lines to my VirtualHost section:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.*\.html$
RewriteRule ^.*$ /index.php?url=%{REQUEST_URI}&%{QUERY_STRING} [L]

e.g.
/news/index.html?var1=value1 -> /index.php?url=/news/index.html&var1=value1

However I noticed that url is not encoded in this case. Is there something
like urlencode() in Apache to fix that? Also are thre any better ways of
handling this?

Best regards, Denis Gerasimov
Outsourcing Services Manager,
VEKOS, Ltd.
www.vekos.ru



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Rewriting all URLs to /index.php

Posted by Denis Gerasimov <de...@vekos.ru>.
Noah,

I finally resolved this task a few days ago (at least it seems to be so :-))
Here is the solution:

<snip>
RewriteEngine on

RewriteLog "E:/intranet/wwwroot/rewrite.log"
RewriteLogLevel 2

RewriteRule ^(.*\/[^/.]+)$ $1/
RewriteRule ^(.*)\/$ $1/index.html [L,R]

RewriteRule ^/news/details/(\d+)\.html$ /news/details.html?id=$1 [QSA]
............

RewriteRule ^(.*\.html)$  /index.php?url=$1 [L,QSA]
</snip>

Anyway, I appreciate your help. Thank you!

> >
> > Which is completely different than your last post. =)
> 
> Well, I just forgot to mention that directory php/ is _outside_ server's
> document root... :-(
> 
> Processing of request is made in the following way:
> 
> 1. index.php creates Application object and calls run() method of it to
> rum
> the application:
> 
> $objApplication = Framework_Application::getInstance();
> $objApplication->run();
> 
> 2. Application takes url parameter and passes it to Dispatcher:
> 
> $objURL = $this->getContext()->getURL();
> return $this->getDispatcher()->dispatch($objURL);
> 
> 3. Dispatcher finds and loads appropriate PHP class, located outside the
> document root, creates Page instance and calls process() method:
> 
> include_once($strFile);
> $objPage = new $strClass($this->getApplication());
> return $objPage->process() && $objPage->display();
> 
> Now you know all the background :-)
> 
> >
> > So, something like this:
> >
> > RewriteEngine on
> > #
> > # place 'news' in $1, 'details' in $2, and the name of the file in $3
> > RewriteRule ^/([^/]+)/([^/]+)/(.*)\.html /php/$1/$2.class.php?id=$3
> > [L,QSA]
> 


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Rewriting all URLs to /index.php

Posted by Denis Gerasimov <de...@vekos.ru>.
> On Thu, Mar 31, 2005 at 10:45:35AM +0400, Denis Gerasimov wrote:
> 
> > > If I'm understand you correctly, something like this:
> > >
> > > RewriteEngine on
> > > RewriteMap unescape int:unescape
> > > RewriteRule ^/news/([^/]+)/([0-9]+)\.html$ \
> > >     /index.php?url=${unescape:/news/$1.html}&id=$2 [L,QSA]
> >
> [snip]
> >
> > The thing is that requested URL, say /demo/test1.html, is internally
> mapped
> > to appropriate file containing PHP class, php/demo/test1.class.php in
> that
> > case. However, I want to have user-friendly URLs like
> /news/details/123.html
> > that should be mapped to php/news/details.class.php, not
> > /news/details/123.class.php
> 
> Which is completely different than your last post. =)

Well, I just forgot to mention that directory php/ is _outside_ server's
document root... :-(

Processing of request is made in the following way:

1. index.php creates Application object and calls run() method of it to rum
the application:

$objApplication = Framework_Application::getInstance();
$objApplication->run();

2. Application takes url parameter and passes it to Dispatcher:

$objURL = $this->getContext()->getURL();
return $this->getDispatcher()->dispatch($objURL);

3. Dispatcher finds and loads appropriate PHP class, located outside the
document root, creates Page instance and calls process() method:

include_once($strFile);
$objPage = new $strClass($this->getApplication());
return $objPage->process() && $objPage->display();

Now you know all the background :-)

> 
> So, something like this:
> 
> RewriteEngine on
> #
> # place 'news' in $1, 'details' in $2, and the name of the file in $3
> RewriteRule ^/([^/]+)/([^/]+)/(.*)\.html /php/$1/$2.class.php?id=$3
> [L,QSA]

No, that is still not a solution :-(

> 
> The assumption here is that the incoming URLs will be in the form:
> 
> /dir/dir/file.html
> 
> If your URLs can be in other formats, other regexes can be developed to
> handle those cases; you may need multiple RewriteRule lines to handle
> them, depending on how your layout.
> 
> --n
> 
> --
> <huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
> <huey> ^^^ Making Flippy Floppy
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Rewriting all URLs to /index.php

Posted by Noah <si...@onastick.net>.
On Thu, Mar 31, 2005 at 10:45:35AM +0400, Denis Gerasimov wrote:

> > If I'm understand you correctly, something like this:
> > 
> > RewriteEngine on
> > RewriteMap unescape int:unescape
> > RewriteRule ^/news/([^/]+)/([0-9]+)\.html$ \
> >     /index.php?url=${unescape:/news/$1.html}&id=$2 [L,QSA]
> 
[snip]
> 
> The thing is that requested URL, say /demo/test1.html, is internally mapped
> to appropriate file containing PHP class, php/demo/test1.class.php in that
> case. However, I want to have user-friendly URLs like /news/details/123.html
> that should be mapped to php/news/details.class.php, not
> /news/details/123.class.php

Which is completely different than your last post. =)

So, something like this:

RewriteEngine on
#
# place 'news' in $1, 'details' in $2, and the name of the file in $3
RewriteRule ^/([^/]+)/([^/]+)/(.*)\.html /php/$1/$2.class.php?id=$3 [L,QSA]

The assumption here is that the incoming URLs will be in the form:

/dir/dir/file.html

If your URLs can be in other formats, other regexes can be developed to
handle those cases; you may need multiple RewriteRule lines to handle
them, depending on how your layout.

--n

-- 
<huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
<huey> ^^^ Making Flippy Floppy


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Rewriting all URLs to /index.php

Posted by Denis Gerasimov <de...@vekos.ru>.
> 
> On Wed, Mar 30, 2005 at 06:08:17PM +0400, Denis Gerasimov wrote:
> 
> > But I've got one more problem. Here is what I need:
> >
> > /news/details/123.html ->
> > /news/details.html?id=123 ->
> > /index.php?url=/news/details.html&id=123
> >
> > I thought the following would work but I was wrong:
> >
> > RewriteEngine on
> > RewriteMap unescape int:unescape
> >
> > RewriteRule ^.*\/$ $1index.html [L,R]
> > RewriteRule ^/news/details/(.+)\.html$ /news/details.html?id=$1 [N]
> > RewriteRule ^.*\.html$  /index.php?url=${unescape:%{REQUEST_URI}}
> [L,QSA]
> >
> > %{REQUEST_URI} is still /news/details/123.html!
> > Should I get rid of %{REQUEST_URI} and do pattern matching instead
> > (something like ^[^?]+\?(.*)$ )?
> 
> Your first RewriteRule may not do what you want, since $1 will be empty
> (no parenthese used in the LHS of that Rule);

Well spotted... however, it works i.e. /news/ -> /news/index.html

> 
> If I'm understand you correctly, something like this:
> 
> RewriteEngine on
> RewriteMap unescape int:unescape
> RewriteRule ^/news/([^/]+)/([0-9]+)\.html$ \
>     /index.php?url=${unescape:/news/$1.html}&id=$2 [L,QSA]

No, that is not what I need :-(. Rewriting to index.php should work
everywhere.

The thing is that requested URL, say /demo/test1.html, is internally mapped
to appropriate file containing PHP class, php/demo/test1.class.php in that
case. However, I want to have user-friendly URLs like /news/details/123.html
that should be mapped to php/news/details.class.php, not
/news/details/123.class.php

> 
> Note that this makes several assumptions:
> 
> 1) all paths you want to rewrite start with /news/;
> 2) the directory element immediately after '/news/' will be the name of
> the html file in the Rewritten path;
> 3) the id is always numeric
> 
> Again, tweak as needed.
> 
> --n
> 
> --
> <huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
> <huey> ^^^ Making Flippy Floppy
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Rewriting all URLs to /index.php

Posted by Noah <si...@onastick.net>.
On Wed, Mar 30, 2005 at 06:08:17PM +0400, Denis Gerasimov wrote:

> But I've got one more problem. Here is what I need:
> 
> /news/details/123.html ->
> /news/details.html?id=123 ->
> /index.php?url=/news/details.html&id=123
> 
> I thought the following would work but I was wrong:
> 
> RewriteEngine on
> RewriteMap unescape int:unescape
> 
> RewriteRule ^.*\/$ $1index.html [L,R]
> RewriteRule ^/news/details/(.+)\.html$ /news/details.html?id=$1 [N]
> RewriteRule ^.*\.html$  /index.php?url=${unescape:%{REQUEST_URI}} [L,QSA]
> 
> %{REQUEST_URI} is still /news/details/123.html!
> Should I get rid of %{REQUEST_URI} and do pattern matching instead
> (something like ^[^?]+\?(.*)$ )?

Your first RewriteRule may not do what you want, since $1 will be empty
(no parenthese used in the LHS of that Rule);

If I'm understand you correctly, something like this:

RewriteEngine on
RewriteMap unescape int:unescape
RewriteRule ^/news/([^/]+)/([0-9]+)\.html$ \
    /index.php?url=${unescape:/news/$1.html}&id=$2 [L,QSA]

Note that this makes several assumptions:

1) all paths you want to rewrite start with /news/;
2) the directory element immediately after '/news/' will be the name of
the html file in the Rewritten path;
3) the id is always numeric

Again, tweak as needed.

--n

-- 
<huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
<huey> ^^^ Making Flippy Floppy


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Rewriting all URLs to /index.php

Posted by Denis Gerasimov <de...@vekos.ru>.
> -----Original Message-----
> From: Noah [mailto:sitz@onastick.net]
> Sent: Wednesday, March 30, 2005 10:30 AM
> To: users@httpd.apache.org
> Subject: Re: [users@httpd] Rewriting all URLs to /index.php
> 
> On Wed, Mar 30, 2005 at 09:58:36AM +0400, Denis Gerasimov wrote:
> 
> > I put the following lines to my VirtualHost section:
> >
> > RewriteEngine On
> > RewriteCond %{REQUEST_FILENAME} ^.*\.html$
> > RewriteRule ^.*$ /index.php?url=%{REQUEST_URI}&%{QUERY_STRING} [L]
> >
> > e.g.
> > /news/index.html?var1=value1 ->
> /index.php?url=/news/index.html&var1=value1
> >
> > However I noticed that url is not encoded in this case. Is there
> something
> > like urlencode() in Apache to fix that? Also are thre any better ways of
> > handling this?
> 
> The RewriteCond line is unnecessary, and you don't need to manually add
> the query string. Remove the RewriteCond line and use the 'QSA'
> RewriteRule flag:
> 
> RewriteEngine on
> RewriteRule ^.*\.html$  /index.php?url=%{REQUEST_URI} [L,QSA]
> 
> If you want to urlescape, you can make use of the internal 'unescape'
> rewritemap:
> 
> RewriteEngine on
> RewriteMap unescape int:unescape
> RewriteRule ^.*\.html$  /index.php?url=${unescape:%{REQUEST_URI}}
> [L,QSA]
> 
> Tweak as needed. =)

Thanks, it works.

But I've got one more problem. Here is what I need:

/news/details/123.html ->
/news/details.html?id=123 ->
/index.php?url=/news/details.html&id=123

I thought the following would work but I was wrong:

RewriteEngine on
RewriteMap unescape int:unescape

RewriteRule ^.*\/$ $1index.html [L,R]
RewriteRule ^/news/details/(.+)\.html$ /news/details.html?id=$1 [N]
RewriteRule ^.*\.html$  /index.php?url=${unescape:%{REQUEST_URI}} [L,QSA]

%{REQUEST_URI} is still /news/details/123.html!
Should I get rid of %{REQUEST_URI} and do pattern matching instead
(something like ^[^?]+\?(.*)$ )?

Does anyone have an idea how to handle this the right way?..

> 
> --n
> 
> --
> <huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
> <huey> ^^^ Making Flippy Floppy
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Rewriting all URLs to /index.php

Posted by Noah <si...@onastick.net>.
On Wed, Mar 30, 2005 at 09:58:36AM +0400, Denis Gerasimov wrote:
 
> I put the following lines to my VirtualHost section:
> 
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} ^.*\.html$
> RewriteRule ^.*$ /index.php?url=%{REQUEST_URI}&%{QUERY_STRING} [L]
> 
> e.g.
> /news/index.html?var1=value1 -> /index.php?url=/news/index.html&var1=value1
> 
> However I noticed that url is not encoded in this case. Is there something
> like urlencode() in Apache to fix that? Also are thre any better ways of
> handling this?

The RewriteCond line is unnecessary, and you don't need to manually add
the query string. Remove the RewriteCond line and use the 'QSA'
RewriteRule flag:

RewriteEngine on
RewriteRule ^.*\.html$  /index.php?url=%{REQUEST_URI} [L,QSA]

If you want to urlescape, you can make use of the internal 'unescape'
rewritemap:

RewriteEngine on
RewriteMap unescape int:unescape
RewriteRule ^.*\.html$  /index.php?url=${unescape:%{REQUEST_URI}}
[L,QSA]

Tweak as needed. =)

--n

-- 
<huey> dd of=/dev/fd0 if=/dev/flippy bs=1024
<huey> ^^^ Making Flippy Floppy


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org