• PHP Help PHP Coding
  • Want to translate hrefs in HTML file from Target.html to engine.php?file=Target.html

I want to take some existing HTML files without changing them, and wrapper them using a PHP script. So far, so good -- all I need to do is include and that's good.

But when these files refer to each other, I'd really like to be able to dynamically change those links so that they point back to the PHP script using the target file as an argument.

Thus... the subject of this post!

Now I want to ensure that it is only local (relative) links that get translated in this way.

It would seem that some of the DOM functions would allow this.

Does anyone have any suggestions?

Thanks!

-- Andrew

    you could do it w/ javascript. but what happens inf javascript is disabled?

    better to use php. look at preg_replace()

      If you're using Apache you could look at mod_rewrite or mod_alias.

        rehfeld -- Would using regular expressions be better than parsing the DOM tree? I definitely want to ignore absolute URLs. Would this be a good way of doing this?

        Weedpacket -- tell me about mod_rewrite and mod_alias. These might be good options. I am running at the root of a conventional apache/php stack, it's my own domain. Have you had good luck with this?

        ALSO: any thought that I could use the <BASE> header directive in my enclosing PHP script? Would that allow a PHP "prefix" to the relative links?

        THANKS AGAIN VERY MUCH for your thoughts!!!

        -- Andrew

          I think rewrite would be a better way, then you really don't have to change the existing code. Just add a .htaccess under the htmls folder

          the content would be like this:

          #start rewrite
          RewriteRule /([a-zA-Z.]*)$ engine.php?file=$1 [L]

          regexp is not tested

          so let us know if it worked

            sorry, left something:

            #start rewrite
            RewriteEngine on
            RewriteRule /([a-zA-Z.]*)$ engine.php?file=$1 [L]

              The rewrite approach is EXACTLY what I want... but I'm testing on my Mac OS X 10.3.7 computer, and the apache.conf file has mod rewrite on, but when I try to put a RewriteRule in -- NOTHING HAPPENS...

              I have put the same .htaccess file into my user root, the whole server root ...

              Do I need to reboot (Apache) to make this work? That doesn't make sense...

                I'm not sure about apache on MAC. But on RedHat, you need to have

                LoadModule rewrite_module modules/mod_rewrite.so
                AddModule mod_rewrite.c

                in http.conf

                  BTW if it's not web root, try this:

                  RewriteEngine on
                  RewriteRule ([a-zA-Z.]*)$ engine.php?file=$1 [L]

                  remove the "/"

                  It worked on my site

                    5 days later

                    OK I have it working on my Mac... Needed to change the order of the Apache mods, and AllowOverride.

                    NOW -- I only need to get my ISP to adjust my heavily customized apache setups 😉

                      Write a Reply...