You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Joao Andrade <jo...@yahoo.com.br> on 2003/10/12 15:16:50 UTC

[users@httpd] Regular expressions in URL rewriting

    Hi there folks,

    That's my very first message to the list, so excuse me if I do any
rookie mistakes. But I'll tell you, I've been through the FAQ and also read
the list archives. Let me first introduce you to the bigger problem.

    What I wanted to do was nothing fancy, my goal was to implement the
concept of "abstract url" as its defined below from an internet textbook
(http://philip.greenspun.com/internet-application-workbook/basics):

>abstract URLs
>As an engineer your primary contributions to an Internet service will be
data model and interaction design (Steps 1 through 3). When you're sketching
the page flow for a discussion >forum on a white board you give the pages
names such as "all-topics", "one-topic", "one-thread", "post-reply",
"post-reply-confirm", etc. Let's call these abstract URLs. Suppose that >you
elect to implement your service in Java Server Pages. Does it make sense to
have the URLs be "all-topics.jsp", "one-topic.jsp", "one-thread.jsp", etc.?
Why should the users see >that you've used JSP? Should they care? And if you
change your mind and switch to Perl, will you change the user-visible URLs
to "all-topics.pl", "one-topic.pl", "one-thread.pl", >etc.? This will break
everyone's bookmarks. More importantly, this change will break all of the
links from other sites to yours. That's a high price to pay for an
implementation >change that should have been invisible to end-users.
>You need a Web programming environment powerful enough that you can build
something that we'll call a request processor. This program looks at an
incoming abstract URL, e.g., >"one-topic", and follows the following logic:

  a.. is there a .jsp file in the file system; if so, execute it
  b.. look for headers requesting WML for a WAP device; if so and there is a
.wml file in the file system, serve it, if not, continue
  c.. look for a .html file
  d.. look for a .jpg
  e.. look for a .gif
(You'll want to customize the preference order for your server.)

    Now to the more specific questions:

    1. Will mod rewrite to the above, or after rewriting an URL from
http://200.155.209.19/basics/hello to http://200.155.209.19/basics/hello.php
will it show the client this last one?

    2. If the answer for "1" is yes would mod_alias be better?

    3. I tried some number of regular expressions with rewrite and none of
them have worked, I fear that there's something missing for its proper work.
I tried

        RewriteRule (.*) $1.php (the actual rule would be more complex, this
was only for test)

        and I still got a 404 error for http://200.155.209.19/basics/hello
(hello.php does exist).

    4. I've read this from a message in the list:

        > RedirectMatch .*^/obit.* http://www.delphosherald.com/obit.php
        >
        > (i.e. 0 or more any characters then anything but a "/" then "obit"
then 0 or more
        > any characters. This should match "/March.obits.html" but not
match "/obit.php".
        > Check up on regular expressions to refine it...)

        How does the program know it has to stop after inserting a .* at the
begining, doesn't it to get any character, any number of times, wouldn't it
go till the end of the line?

    5.  What's with the '^' and '$' anyway? I've seen this expression in the
docs:

        ^/$

        one tells to align at he beginning of a line and the other to align
at the end. What's the sense in of it?

        That's it I hope to have been clear enough. Thanx a lot for your
attention.

        Joao.






---------------------------------------------------------------------
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] Regular expressions in URL rewriting

Posted by Norman Peelman <np...@cfl.rr.com>.
>     I guess it would norm. I knew a HTML page can be treated as a PHP
> aplication, I just didn't know this would also work for Java and Perl, I
> have never worked with those. Anyway, after have been working for three
days
> in it, soon after posting the problem I was able to solve it:
>
> RewriteEngine on
> # The rewritelog was used for debugging, now that it works
> # the loglevel is set to 0 -  no log
> RewriteLog "C:/Programme/Apache Group/Apache2/logs/rewrite.log"
> RewriteLogLevel 0
> # those rewriting rules are used to implement the concept of
> # abstract urls as stated in
> http://philip.greenspun.com/internet-application-workbook/basics
> RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1" -d   checks if
> there is such a directory
> RewriteRule (.*[^/])$ $1/ [L]    if there is a trailing slash is added,
> RewriteRule (.*[^/])$ $1.php   if not the extension your working is added
>
> This has only been done that way because HTML can be treated as PHP, if I
> used Perl and it required a diffent treatment as to HTML than it would
look
> like this:
>
> RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1.html" -f   checks
if
> there is such a FILE
> RewriteRule (.*[^/])$ $1.html [L]    if there is .html is added,
>
> RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1.pl" -f   checks if
> there is such a FILE
> RewriteRule (.*[^/])$ $1.pl [L]    if there is .pl is added
>
> Thanx anyway for the help.
>
>
> ----- Original Message -----
> From: "Norman Peelman" <np...@cfl.rr.com>
> To: <us...@httpd.apache.org>; "Joao Andrade" <jo...@yahoo.com.br>
> Sent: Sunday, October 12, 2003 12:03 PM
> Subject: Re: [users@httpd] Regular expressions in URL rewriting
>
> >
> >   Would'nt it be easier to just AddType .htm and .html to whatever
> languages
> > you are using?
> > AddType application/x-httpd-php .php .htm .html
> > AddType application/x-httpd-jsp .jsp .htm .html
> > AddType application/x-httpd-pl .pl .htm .html
> > etc.
> >
> > or would this not work?
> >
> > or at least just assign .htm and .html to the language you are currently
> > using?
> >
> > Norm

   I would guess that assigning .htm and .html to more than one language
would confuse Apache (or at least it would use the first one encountered -
or maybe the last one encountered, in the config file). But I would imagine
it would work on any language.

Norm


---------------------------------------------------------------------
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] Regular expressions in URL rewriting

Posted by Joao Andrade <jo...@yahoo.com.br>.
    I guess it would norm. I knew a HTML page can be treated as a PHP
aplication, I just didn't know this would also work for Java and Perl, I
have never worked with those. Anyway, after have been working for three days
in it, soon after posting the problem I was able to solve it:

RewriteEngine on
# The rewritelog was used for debugging, now that it works
# the loglevel is set to 0 -  no log
RewriteLog "C:/Programme/Apache Group/Apache2/logs/rewrite.log"
RewriteLogLevel 0
# those rewriting rules are used to implement the concept of
# abstract urls as stated in
http://philip.greenspun.com/internet-application-workbook/basics
RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1" -d   checks if
there is such a directory
RewriteRule (.*[^/])$ $1/ [L]    if there is a trailing slash is added,
RewriteRule (.*[^/])$ $1.php   if not the extension your working is added

This has only been done that way because HTML can be treated as PHP, if I
used Perl and it required a diffent treatment as to HTML than it would look
like this:

RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1.html" -f   checks if
there is such a FILE
RewriteRule (.*[^/])$ $1.html [L]    if there is .html is added,

RewriteCond "C:/Programme/Apache Group/Apache2/htdocs$1.pl" -f   checks if
there is such a FILE
RewriteRule (.*[^/])$ $1.pl [L]    if there is .pl is added

Thanx anyway for the help.


----- Original Message -----
From: "Norman Peelman" <np...@cfl.rr.com>
To: <us...@httpd.apache.org>; "Joao Andrade" <jo...@yahoo.com.br>
Sent: Sunday, October 12, 2003 12:03 PM
Subject: Re: [users@httpd] Regular expressions in URL rewriting

>
>   Would'nt it be easier to just AddType .htm and .html to whatever
languages
> you are using?
> AddType application/x-httpd-php .php .htm .html
> AddType application/x-httpd-jsp .jsp .htm .html
> AddType application/x-httpd-pl .pl .htm .html
> etc.
>
> or would this not work?
>
> or at least just assign .htm and .html to the language you are currently
> using?
>
> Norm
>



> >     Hi there folks,
> >
> >     That's my very first message to the list, so excuse me if I do any
> > rookie mistakes. But I'll tell you, I've been through the FAQ and also
> read
> > the list archives. Let me first introduce you to the bigger problem.
> >
> >     What I wanted to do was nothing fancy, my goal was to implement the
> > concept of "abstract url" as its defined below from an internet textbook
> > (http://philip.greenspun.com/internet-application-workbook/basics):
> >
> > >abstract URLs
> > >As an engineer your primary contributions to an Internet service will
be
> > data model and interaction design (Steps 1 through 3). When you're
> sketching
> > the page flow for a discussion >forum on a white board you give the
pages
> > names such as "all-topics", "one-topic", "one-thread", "post-reply",
> > "post-reply-confirm", etc. Let's call these abstract URLs. Suppose that
> >you
> > elect to implement your service in Java Server Pages. Does it make sense
> to
> > have the URLs be "all-topics.jsp", "one-topic.jsp", "one-thread.jsp",
> etc.?
> > Why should the users see >that you've used JSP? Should they care? And if
> you
> > change your mind and switch to Perl, will you change the user-visible
URLs
> > to "all-topics.pl", "one-topic.pl", "one-thread.pl", >etc.? This will
> break
> > everyone's bookmarks. More importantly, this change will break all of
the
> > links from other sites to yours. That's a high price to pay for an
> > implementation >change that should have been invisible to end-users.
> > >You need a Web programming environment powerful enough that you can
build
> > something that we'll call a request processor. This program looks at an
> > incoming abstract URL, e.g., >"one-topic", and follows the following
> logic:
> >
> >   a.. is there a .jsp file in the file system; if so, execute it
> >   b.. look for headers requesting WML for a WAP device; if so and there
is
> a
> > .wml file in the file system, serve it, if not, continue
> >   c.. look for a .html file
> >   d.. look for a .jpg
> >   e.. look for a .gif
> > (You'll want to customize the preference order for your server.)
> >
> >     Now to the more specific questions:
> >
> >     1. Will mod rewrite to the above, or after rewriting an URL from
> > http://200.155.209.19/basics/hello to
> http://200.155.209.19/basics/hello.php
> > will it show the client this last one?
> >
> >     2. If the answer for "1" is yes would mod_alias be better?
> >
> >     3. I tried some number of regular expressions with rewrite and none
of
> > them have worked, I fear that there's something missing for its proper
> work.
> > I tried
> >
> >         RewriteRule (.*) $1.php (the actual rule would be more complex,
> this
> > was only for test)
> >
> >         and I still got a 404 error for
http://200.155.209.19/basics/hello
> > (hello.php does exist).
> >
> >     4. I've read this from a message in the list:
> >
> >         > RedirectMatch .*^/obit.* http://www.delphosherald.com/obit.php
> >         >
> >         > (i.e. 0 or more any characters then anything but a "/" then
> "obit"
> > then 0 or more
> >         > any characters. This should match "/March.obits.html" but not
> > match "/obit.php".
> >         > Check up on regular expressions to refine it...)
> >
> >         How does the program know it has to stop after inserting a .* at
> the
> > begining, doesn't it to get any character, any number of times, wouldn't
> it
> > go till the end of the line?
> >
> >     5.  What's with the '^' and '$' anyway? I've seen this expression in
> the
> > docs:
> >
> >         ^/$
> >
> >         one tells to align at he beginning of a line and the other to
> align
> > at the end. What's the sense in of it?
> >
> >         That's it I hope to have been clear enough. Thanx a lot for your
> > attention.
> >
> >         Joao.




---------------------------------------------------------------------
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] Regular expressions in URL rewriting

Posted by Norman Peelman <np...@cfl.rr.com>.
>     Hi there folks,
>
>     That's my very first message to the list, so excuse me if I do any
> rookie mistakes. But I'll tell you, I've been through the FAQ and also
read
> the list archives. Let me first introduce you to the bigger problem.
>
>     What I wanted to do was nothing fancy, my goal was to implement the
> concept of "abstract url" as its defined below from an internet textbook
> (http://philip.greenspun.com/internet-application-workbook/basics):
>
> >abstract URLs
> >As an engineer your primary contributions to an Internet service will be
> data model and interaction design (Steps 1 through 3). When you're
sketching
> the page flow for a discussion >forum on a white board you give the pages
> names such as "all-topics", "one-topic", "one-thread", "post-reply",
> "post-reply-confirm", etc. Let's call these abstract URLs. Suppose that
>you
> elect to implement your service in Java Server Pages. Does it make sense
to
> have the URLs be "all-topics.jsp", "one-topic.jsp", "one-thread.jsp",
etc.?
> Why should the users see >that you've used JSP? Should they care? And if
you
> change your mind and switch to Perl, will you change the user-visible URLs
> to "all-topics.pl", "one-topic.pl", "one-thread.pl", >etc.? This will
break
> everyone's bookmarks. More importantly, this change will break all of the
> links from other sites to yours. That's a high price to pay for an
> implementation >change that should have been invisible to end-users.
> >You need a Web programming environment powerful enough that you can build
> something that we'll call a request processor. This program looks at an
> incoming abstract URL, e.g., >"one-topic", and follows the following
logic:
>
>   a.. is there a .jsp file in the file system; if so, execute it
>   b.. look for headers requesting WML for a WAP device; if so and there is
a
> .wml file in the file system, serve it, if not, continue
>   c.. look for a .html file
>   d.. look for a .jpg
>   e.. look for a .gif
> (You'll want to customize the preference order for your server.)
>
>     Now to the more specific questions:
>
>     1. Will mod rewrite to the above, or after rewriting an URL from
> http://200.155.209.19/basics/hello to
http://200.155.209.19/basics/hello.php
> will it show the client this last one?
>
>     2. If the answer for "1" is yes would mod_alias be better?
>
>     3. I tried some number of regular expressions with rewrite and none of
> them have worked, I fear that there's something missing for its proper
work.
> I tried
>
>         RewriteRule (.*) $1.php (the actual rule would be more complex,
this
> was only for test)
>
>         and I still got a 404 error for http://200.155.209.19/basics/hello
> (hello.php does exist).
>
>     4. I've read this from a message in the list:
>
>         > RedirectMatch .*^/obit.* http://www.delphosherald.com/obit.php
>         >
>         > (i.e. 0 or more any characters then anything but a "/" then
"obit"
> then 0 or more
>         > any characters. This should match "/March.obits.html" but not
> match "/obit.php".
>         > Check up on regular expressions to refine it...)
>
>         How does the program know it has to stop after inserting a .* at
the
> begining, doesn't it to get any character, any number of times, wouldn't
it
> go till the end of the line?
>
>     5.  What's with the '^' and '$' anyway? I've seen this expression in
the
> docs:
>
>         ^/$
>
>         one tells to align at he beginning of a line and the other to
align
> at the end. What's the sense in of it?
>
>         That's it I hope to have been clear enough. Thanx a lot for your
> attention.
>
>         Joao.

  Would'nt it be easier to just AddType .htm and .html to whatever languages
you are using?
AddType application/x-httpd-php .php .htm .html
AddType application/x-httpd-jsp .jsp .htm .html
AddType application/x-httpd-pl .pl .htm .html
etc.

or would this not work?

or at least just assign .htm and .html to the language you are currently
using?

Norm


---------------------------------------------------------------------
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] Regular expressions in URL rewriting

Posted by Robert Andersson <ro...@profundis.nu>.
Joao Andrade wrote:
>     Now to the more specific questions:
>
>     1. Will mod rewrite to the above, or after rewriting an URL from
> http://200.155.209.19/basics/hello to
> http://200.155.209.19/basics/hello.php will it show the client this last
one?

You don't need mod_rewrite. What does exactly this is mod_negotiation, which
takes care of Content-negotiation, see:
http://httpd.apache.org/docs-2.0/content-negotiation.html

What you do is to load the module, and have this in a directory:

    Options +MultiViews

This makes it possible to make URIs without the extension, and Apache will
find the appropriate file, by rules which you don't need to worry about if
you only have one anyway.

Regards,
Robert Andersson


---------------------------------------------------------------------
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