can you redirect every existing html page through a php function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Kay

    can you redirect every existing html page through a php function?

    Is there a way to redirect every single page on an existing web site through
    a php function?

    In other words, say I have a whole functional HTML web site, never written
    to use any php. Now I would like to have a php function handle displaying
    every page, for example, instead of:



    you would call:



    ....and then the php could do some fancier includes at the end of each page,
    etc., or add a header, etc.

    I don't want to rewrite the whole site and rename all the pages; also, this
    site has been around for awhile and there are a lot of links floating around
    the internet to direct urls on the site, so I'm wondering if I can intercept
    every URL to that site and redirect it to the php function, which will then
    open the page.

    Thanks for any advice on this...
    --
    Stephen Kay
    Karma-Lab sk@karma-lab.NOSPAM.com
    ^^^^^^^


  • Simon

    #2
    Re: can you redirect every existing html page through a php function?

    > Is there a way to redirect every single page on an existing web site[color=blue]
    > through
    > a php function?
    >
    > In other words, say I have a whole functional HTML web site, never written
    > to use any php. Now I would like to have a php function handle displaying
    > every page, for example, instead of:
    >
    > http://www.mysite.com/mypage.html
    >
    > you would call:
    >
    > http://www.mysite.com/dopage.php?p=mypage.html
    >
    > ...and then the php could do some fancier includes at the end of each
    > page,
    > etc., or add a header, etc.
    >
    > I don't want to rewrite the whole site and rename all the pages; also,
    > this
    > site has been around for awhile and there are a lot of links floating
    > around
    > the internet to direct urls on the site, so I'm wondering if I can
    > intercept
    > every URL to that site and redirect it to the php function, which will
    > then
    > open the page.
    >
    > Thanks for any advice on this...
    > --
    > Stephen Kay
    > Karma-Lab sk@karma-lab.NOSPAM.com
    > ^^^^^^^[/color]

    What server are you using?
    If you are using apache then you can use RewriteRule ... It will do exactly
    what you are after.

    RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]

    Then you can worry about what dopage.php actually does.

    Simon
    --
    Own this domain today. We make your shopping experience easy. Friendly and quick customer service.

    Free URL redirection service. Turns a long URL into a much shorter one.


    Comment

    • d

      #3
      Re: can you redirect every existing html page through a php function?

      "Stephen Kay" <sk@karma-lab.nospam.com> wrote in message
      news:C04D0075.5 A945%sk@karma-lab.nospam.com. ..[color=blue]
      > Is there a way to redirect every single page on an existing web site
      > through
      > a php function?
      >
      > In other words, say I have a whole functional HTML web site, never written
      > to use any php. Now I would like to have a php function handle displaying
      > every page, for example, instead of:
      >
      > http://www.mysite.com/mypage.html
      >
      > you would call:
      >
      > http://www.mysite.com/dopage.php?p=mypage.html
      >
      > ...and then the php could do some fancier includes at the end of each
      > page,
      > etc., or add a header, etc.
      >
      > I don't want to rewrite the whole site and rename all the pages; also,
      > this
      > site has been around for awhile and there are a lot of links floating
      > around
      > the internet to direct urls on the site, so I'm wondering if I can
      > intercept
      > every URL to that site and redirect it to the php function, which will
      > then
      > open the page.
      >
      > Thanks for any advice on this...
      > --
      > Stephen Kay
      > Karma-Lab sk@karma-lab.NOSPAM.com
      > ^^^^^^^[/color]

      If you're running Apache, you could use a rewrite rule to redirect all
      requests for .html files which exist like this:

      RewriteEngine On
      RewriteCond /var/www/docroot%{REQUES T_URI} -f
      RewriteRule .* /var/www/docroot/fancy.php [L,NS]

      (untested, btw)

      and in fancy.php look at the $_SERVER["REQUEST_UR I"] variable to get the
      name of the HTML which was requested. The RewriteCond will ensure the PHP
      is only called when the page exists. If you want to have 404 messages, you
      can remove that line, and maybe check for just .html. Personally, I have
      all requests for non-existing files going through a PHP file, to handle
      site-wide database connections, templates, etc.

      hope that helps!

      dave


      Comment

      • Stephen Kay

        #4
        Re: can you redirect every existing html page through a phpfunction?

        [color=blue]
        > If you're running Apache, you could use a rewrite rule to redirect all
        > requests for .html files which exist like this:
        >
        > RewriteEngine On
        > RewriteCond /var/www/docroot%{REQUES T_URI} -f
        > RewriteRule .* /var/www/docroot/fancy.php [L,NS]
        >
        > (untested, btw)
        >
        > and in fancy.php look at the $_SERVER["REQUEST_UR I"] variable to get the
        > name of the HTML which was requested. The RewriteCond will ensure the PHP
        > is only called when the page exists. If you want to have 404 messages, you
        > can remove that line, and maybe check for just .html. Personally, I have
        > all requests for non-existing files going through a PHP file, to handle
        > site-wide database connections, templates, etc.[/color]


        Thanks dave -

        [color=blue]
        > What server are you using?
        > If you are using apache then you can use RewriteRule ... It will do exactly
        > what you are after.
        >
        > RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]
        >
        > Then you can worry about what dopage.php actually does.[/color]


        Thanks Simon -

        I'll check them out!

        --
        Stephen Kay
        Karma-Lab sk@karma-lab.NOSPAM.com
        ^^^^^^^

        Comment

        • Stephen Kay

          #5
          Re: can you redirect every existing html page through a phpfunction?

          in article 48pjs3FldgsjU1@ individual.net, Simon at spambucket@exam ple.com
          wrote on 3/27/06 2:53 AM:
          [color=blue]
          > What server are you using?
          > If you are using apache then you can use RewriteRule ... It will do exactly
          > what you are after.
          >
          > RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]
          >
          > Then you can worry about what dopage.php actually does.[/color]

          Hi Simon,
          I've been fooling around with this, and it shows signs of working for things
          like:



          However, it doesn't work for something that is in the root html folder,



          Since I don't understand the syntax (yet), any suggestions?

          Thanks!
          --
          Stephen Kay
          Karma-Lab sk@karma-lab.NOSPAM.com
          ^^^^^^^


          Comment

          • Stephen Kay

            #6
            Re: can you redirect every existing html page through a phpfunction?

            in article C04F1F23.5AE27% sk@karma-lab.nospam.com, Stephen Kay at
            sk@karma-lab.nospam.com wrote on 3/28/06 5:16 PM:
            [color=blue][color=green]
            >> What server are you using?
            >> If you are using apache then you can use RewriteRule ... It will do exactly
            >> what you are after.
            >>
            >> RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]
            >>
            >> Then you can worry about what dopage.php actually does.[/color][/color]

            I said:
            [color=blue]
            > Hi Simon,
            > I've been fooling around with this, and it shows signs of working for things
            > like:
            >
            > www.mysite.com/folder/somepage.html
            > However, it doesn't work for something that is in the root html folder,
            > www.mysite.com/somepage.html
            > Since I don't understand the syntax (yet), any suggestions?[/color]


            I see, it's something like:

            RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1
            RewriteRule ^/([^/]+).html$ /dopage.php?p=$1 [L]

            But the problem I'm having with this is that, if I rewrite all URLs and send
            them to dopage.php, and then in dopage.php I'm calling curl to open a page
            of my website, at that point it gets redirected the same way and there's an
            infinite loop.


            --
            Stephen Kay
            Karma-Lab sk@karma-lab.NOSPAM.com
            ^^^^^^^


            Comment

            • The Numerator

              #7
              Re: can you redirect every existing html page through a php function?

              Use this code:

              <html>
              <head>
              <title>CWO</title>
              <base href="http://www.christwarri ors.net/stuff/index.html">
              </head>
              <body>
              <?php

              $p = $_GET['p']; // This sets the variable

              if ($_GET['title'] == "") // If its only dopage.php,
              {
              include "index.html "; // show index.html (problem is that all the links
              are relative to the php file not the html)
              }

              elseif ($_GET['p'] == "mysite.htm l") // If the URL is
              dopage.php?p=my site.html,
              {
              include "mysite.htm l"; // show mysite.html
              }

              else // if its none of the above
              {
              include "invalid.ht ml"; // show the page invalid.html
              }

              ?>
              </body>
              </html>

              the only problem is that the links are relative only to the php...but
              you can easily work around that

              Comment

              • michael@tougeron.com

                #8
                Re: can you redirect every existing html page through a php function?

                > But the problem I'm having with this is that, if I rewrite all URLs and send[color=blue]
                > them to dopage.php, and then in dopage.php I'm calling curl to open a page
                > of my website, at that point it gets redirected the same way and there's an
                > infinite loop.[/color]

                I'm confused why you're using curl to get the page content. Is there a
                specific reason for it that using "include (filename);" would not
                handle?

                If you need to use curl, I'd recommend checking out Apache's
                documentation on rewrite rules at
                http://httpd.apache.org/docs/2.2/rew...ite_guide.html Check out
                the areas in regards to rewritecond while will conditionally allow you
                to rewrite the url.

                Comment

                • Stephen Kay

                  #9
                  Is my understanding of eval incorrect?

                  I thought from my brief reading about eval() that you could take an html
                  file that had some php variables in it, such as placeholders for links, and
                  then turn it into an html version with the correct links, by doing something
                  like:

                  $html = implode('', file('http://www.mysite.com/transbar.html') );

                  // populate variable names with links

                  eval ("\$html = \"$html\";") ;
                  echo $html;


                  ....but I get parse errors from eval. Obviously it's not that simple...?

                  --
                  Stephen Kay
                  Karma-Lab sk@karma-lab.NOSPAM.com
                  ^^^^^^^


                  Comment

                  • d

                    #10
                    Re: Is my understanding of eval incorrect?

                    "Stephen Kay" <sk@karma-lab.nospam.com> wrote in message
                    news:C04F9F58.5 AF7A%sk@karma-lab.nospam.com. ..[color=blue]
                    >I thought from my brief reading about eval() that you could take an html
                    > file that had some php variables in it, such as placeholders for links,
                    > and
                    > then turn it into an html version with the correct links, by doing
                    > something
                    > like:
                    >
                    > $html = implode('', file('http://www.mysite.com/transbar.html') );
                    >
                    > // populate variable names with links
                    >
                    > eval ("\$html = \"$html\";") ;
                    > echo $html;
                    >
                    >
                    > ...but I get parse errors from eval. Obviously it's not that simple...?[/color]

                    "If eval is the answer, you're asking the wrong question" :)
                    [color=blue]
                    > --
                    > Stephen Kay
                    > Karma-Lab sk@karma-lab.NOSPAM.com
                    > ^^^^^^^
                    >
                    >[/color]


                    Comment

                    • Stephen Kay

                      #11
                      Re: Is my understanding of eval incorrect?

                      in article IkwWf.44142$wl. 11344@text.news .blueyonder.co. uk, d at
                      d@example.com wrote on 3/29/06 8:54 AM:
                      [color=blue][color=green]
                      >> I thought from my brief reading about eval() that you could take an html
                      >> file that had some php variables in it, such as placeholders for links,
                      >> and
                      >> then turn it into an html version with the correct links, by doing
                      >> something
                      >> like:
                      >>
                      >> $html = implode('', file('http://www.mysite.com/transbar.html') );
                      >>
                      >> // populate variable names with links
                      >>
                      >> eval ("\$html = \"$html\";") ;
                      >> echo $html;
                      >>
                      >>
                      >> ...but I get parse errors from eval. Obviously it's not that simple...?[/color][/color]

                      [color=blue]
                      > "If eval is the answer, you're asking the wrong question" :)[/color]


                      Well, that was just so......helpful . ;)

                      OK, look: here's another way of asking the question:

                      How do I load an html file containing buttons that reference php variables,
                      and then populate the variables with the correct values, and send it out as
                      HTML?

                      I thought that eval() might be part of the solution, but you seem to be
                      implying that it is not.

                      --
                      Stephen Kay
                      Karma-Lab sk@karma-lab.NOSPAM.com
                      ^^^^^^^


                      Comment

                      • Stephen Kay

                        #12
                        Re: can you redirect every existing html page through a phpfunction?

                        in article C04E2935.5AC82% sk@karma-lab.nospam.com, Stephen Kay at
                        sk@karma-lab.nospam.com wrote on 3/27/06 11:46 PM:
                        [color=blue][color=green]
                        >> What server are you using?
                        >> If you are using apache then you can use RewriteRule ... It will do exactly
                        >> what you are after.
                        >>
                        >> RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]
                        >>
                        >> Then you can worry about what dopage.php actually does.[/color][/color]

                        Hi, I've been working on this, and it's working.

                        But presently, I'm doing this:

                        RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+).html$
                        /dopage3.php?p=$ 1/$2/$3/$4.html
                        RewriteRule ^([^/]+)/([^/]+)/([^/]+).html$ /dopage3.php?p=$ 1/$2/$3.html
                        RewriteRule ^([^/]+)/([^/]+).html$ /dopage3.php?p=$ 1/$2.html
                        RewriteRule ^([^/]+).html$ /dopage3.php?p=$ 1.html [L]


                        This looks really stupid to me, but I haven't come on a better syntax.

                        In other words, it needs the first line to redirect something like:

                        folder1/folder2/folder3/file.html to
                        /dopage.php?p= folder1/folder2/folder3/file.html

                        and then the next line to do

                        folder1/folder2/file.html to
                        /dopage.php?p= folder1/folder2/file.html

                        ...etc., depending on how many layers down the html is nested.

                        Is there a simpler way to write this that only takes one line? I would have
                        thought something like this (based on my limited understanding), but it
                        doesn't seem to work exactly the same:

                        RewriteRule ^([*]).html$ /dopage3.php?p=$ 1.html [L]



                        --
                        Stephen Kay
                        Karma-Lab sk@karma-lab.NOSPAM.com
                        ^^^^^^^


                        Comment

                        • d

                          #13
                          Re: Is my understanding of eval incorrect?

                          "Stephen Kay" <sk@karma-lab.nospam.com> wrote in message
                          news:C0503716.5 B05A%sk@karma-lab.nospam.com. ..[color=blue]
                          > in article IkwWf.44142$wl. 11344@text.news .blueyonder.co. uk, d at
                          > d@example.com wrote on 3/29/06 8:54 AM:
                          >[color=green][color=darkred]
                          >>> I thought from my brief reading about eval() that you could take an html
                          >>> file that had some php variables in it, such as placeholders for links,
                          >>> and
                          >>> then turn it into an html version with the correct links, by doing
                          >>> something
                          >>> like:
                          >>>
                          >>> $html = implode('', file('http://www.mysite.com/transbar.html') );
                          >>>
                          >>> // populate variable names with links
                          >>>
                          >>> eval ("\$html = \"$html\";") ;
                          >>> echo $html;
                          >>>
                          >>>
                          >>> ...but I get parse errors from eval. Obviously it's not that simple...?[/color][/color]
                          >
                          >[color=green]
                          >> "If eval is the answer, you're asking the wrong question" :)[/color]
                          >
                          >
                          > Well, that was just so......helpful . ;)
                          >
                          > OK, look: here's another way of asking the question:
                          >
                          > How do I load an html file containing buttons that reference php
                          > variables,
                          > and then populate the variables with the correct values, and send it out
                          > as
                          > HTML?[/color]

                          include() :)
                          [color=blue]
                          > I thought that eval() might be part of the solution, but you seem to be
                          > implying that it is not.
                          >
                          > --
                          > Stephen Kay
                          > Karma-Lab sk@karma-lab.NOSPAM.com
                          > ^^^^^^^
                          >
                          >[/color]


                          Comment

                          • d

                            #14
                            Re: can you redirect every existing html page through a phpfunction?

                            "Stephen Kay" <sk@karma-lab.nospam.com> wrote in message
                            news:C0521A79.5 B343%sk@karma-lab.nospam.com. ..[color=blue]
                            > in article C04E2935.5AC82% sk@karma-lab.nospam.com, Stephen Kay at
                            > sk@karma-lab.nospam.com wrote on 3/27/06 11:46 PM:
                            >[color=green][color=darkred]
                            >>> What server are you using?
                            >>> If you are using apache then you can use RewriteRule ... It will do
                            >>> exactly
                            >>> what you are after.
                            >>>
                            >>> RewriteRule ^([^/]+)/([^/]+).html$ /dopage.php?p=$1 [L]
                            >>>
                            >>> Then you can worry about what dopage.php actually does.[/color][/color]
                            >
                            > Hi, I've been working on this, and it's working.
                            >
                            > But presently, I'm doing this:
                            >
                            > RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+).html$
                            > /dopage3.php?p=$ 1/$2/$3/$4.html
                            > RewriteRule ^([^/]+)/([^/]+)/([^/]+).html$ /dopage3.php?p=$ 1/$2/$3.html
                            > RewriteRule ^([^/]+)/([^/]+).html$ /dopage3.php?p=$ 1/$2.html
                            > RewriteRule ^([^/]+).html$ /dopage3.php?p=$ 1.html [L]
                            >
                            >
                            > This looks really stupid to me, but I haven't come on a better syntax.
                            >
                            > In other words, it needs the first line to redirect something like:
                            >
                            > folder1/folder2/folder3/file.html to
                            > /dopage.php?p= folder1/folder2/folder3/file.html
                            >
                            > and then the next line to do
                            >
                            > folder1/folder2/file.html to
                            > /dopage.php?p= folder1/folder2/file.html
                            >
                            > ..etc., depending on how many layers down the html is nested.
                            >
                            > Is there a simpler way to write this that only takes one line? I would
                            > have
                            > thought something like this (based on my limited understanding), but it
                            > doesn't seem to work exactly the same:
                            >
                            > RewriteRule ^([*]).html$ /dopage3.php?p=$ 1.html [L][/color]

                            You can do this:

                            RewriteCond /var/www/htdocs/static%{REQUEST _URI} -f
                            RewriteRule (.*) /static$1 [L]
                            RewriteRule .* /dostuff.php [NS,L]

                            and every file that exists in the static/ subdirectory will be served as if
                            it was in the root, and all requests to files that do not exist in there are
                            sent to the dostuff.php. You don't have to worry about breaking the URI
                            apart in the rewrite configuring, as you can explode()
                            $_SERVER["REQUEST_UR I"] in dostuff.php to see what exactly was requested.

                            If you don't want to have your static content in a static subdirectory, you
                            can do this:

                            RewriteCond /var/www/htdocs%{REQUEST _URI} !-f
                            RewriteRule .* /dostuff.php [NS,L]

                            hope that helps!

                            dave
                            [color=blue]
                            >
                            >
                            > --
                            > Stephen Kay
                            > Karma-Lab sk@karma-lab.NOSPAM.com
                            > ^^^^^^^
                            >
                            >[/color]


                            Comment

                            Working...