.htaccess and PHP

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

    .htaccess and PHP

    Just looking for some general advice on modifying the URL for php.

    I am using .htaccess to allow a web site to translate example.html to
    index.php?conte nt=example
    (below)

    ErrorDocument 404 http://www.example.com
    RewriteEngine on
    RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]

    Is there any downside to using this method. I just want to know before I
    roll it out over several other sites.

    Nel.


  • Justin Koivisto

    #2
    Re: .htaccess and PHP

    Nel wrote:[color=blue]
    > Just looking for some general advice on modifying the URL for php.
    >
    > I am using .htaccess to allow a web site to translate example.html to
    > index.php?conte nt=example
    > (below)
    >
    > ErrorDocument 404 http://www.example.com
    > RewriteEngine on
    > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
    >
    > Is there any downside to using this method. I just want to know before I
    > roll it out over several other sites.[/color]

    This is a similar approach to what I've used on a handful of sites
    (20-30). I haven't experienced any downside to this yet - unless there
    is a problem on the server with the mod_rewrite or you are going to a
    server other than Apache like zues or *gasp* IIS - in which case you'd
    have to hope that ISAPI_rewrite and whatever Zues uses is compatible
    enough to get you through.

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Edward Alfert

      #3
      Re: .htaccess and PHP

      "Nel" <nelly@ne14.co. NOSPAMuk> wrote in news:DwGxc.1289 4$NK4.1841718
      @stones.force9. net:
      [color=blue]
      > Just looking for some general advice on modifying the URL for php.
      >
      > I am using .htaccess to allow a web site to translate example.html to
      > index.php?conte nt=example
      > (below)
      >
      > ErrorDocument 404 http://www.example.com
      > RewriteEngine on
      > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
      >
      > Is there any downside to using this method. I just want to know[/color]
      before I[color=blue]
      > roll it out over several other sites.
      >
      > Nel.
      >[/color]

      Nel,

      I use something similar.
      1) I redirect alias domains and domains without www. in front to my main
      domain using a 301 redirect which is what Google likes in order to
      maintain pagerank.
      2) I test to make sure the page being requested is not a file or
      directory that exists. If it exists, then retrive the existing page.
      This is nice in case you want to have a pages that are not part of your
      content management system.
      3) I don't just redirect pages with .html extensions. I redirect
      everything into content management system and then the cms parses the
      requested page and strips extensions, trailing slashes, etc.



      Options +SymlinksIfOwne rMatch

      RewriteEngine On

      RewriteCond %{HTTP_HOST} !^www\.rootmode \.com
      RewriteRule ^(.*)$ http://www\.rootmode\. com/$1 [R=
      301,L]

      RewriteCond %{REQUEST_FILEN AME} -f [OR]
      RewriteCond %{REQUEST_FILEN AME} -d
      RewriteRule ^(.*)$ $1 [L]

      RewriteCond %{REQUEST_URI} !^/index\.php
      RewriteRule ^(.*)$ /index.php?page= $1 [L]


      --
      Edward Alfert

      Multiple Domain Hosting and Reseller Hosting Plans
      Coupon Code (Recurring $5/month Discount): newsgroup

      Comment

      • Chung Leong

        #4
        Re: .htaccess and PHP

        "Nel" <nelly@ne14.co. NOSPAMuk> wrote in message
        news:DwGxc.1289 4$NK4.1841718@s tones.force9.ne t...[color=blue]
        > Just looking for some general advice on modifying the URL for php.
        >
        > I am using .htaccess to allow a web site to translate example.html to
        > index.php?conte nt=example
        > (below)
        >
        > ErrorDocument 404 http://www.example.com
        > RewriteEngine on
        > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
        >
        > Is there any downside to using this method. I just want to know before I
        > roll it out over several other sites.[/color]

        Other then losing the ability to use regular .html files, there's no major
        downside I can think of. Then again, there's little upside to doing things
        this way. If you don't need to use Apache rewrite, my advise is to not use
        it.


        Comment

        • Nel

          #5
          Re: .htaccess and PHP

          "Edward Alfert" <ealfert@rootmo de.com> wrote in message
          news:Xns9503CBB 689139ealfertro otmodecom@130.1 33.1.4...[color=blue]
          > "Nel" <nelly@ne14.co. NOSPAMuk> wrote in news:DwGxc.1289 4$NK4.1841718
          > @stones.force9. net:
          >[color=green]
          > > Just looking for some general advice on modifying the URL for php.
          > >
          > > I am using .htaccess to allow a web site to translate example.html to
          > > index.php?conte nt=example
          > > (below)
          > >
          > > ErrorDocument 404 http://www.example.com
          > > RewriteEngine on
          > > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
          > >
          > > Is there any downside to using this method. I just want to know[/color]
          > before I[color=green]
          > > roll it out over several other sites.
          > >
          > > Nel.
          > >[/color]
          >
          > Nel,
          >
          > I use something similar.
          > 1) I redirect alias domains and domains without www. in front to my main
          > domain using a 301 redirect which is what Google likes in order to
          > maintain pagerank.
          > 2) I test to make sure the page being requested is not a file or
          > directory that exists. If it exists, then retrive the existing page.
          > This is nice in case you want to have a pages that are not part of your
          > content management system.
          > 3) I don't just redirect pages with .html extensions. I redirect
          > everything into content management system and then the cms parses the
          > requested page and strips extensions, trailing slashes, etc.
          >
          >
          >
          > Options +SymlinksIfOwne rMatch
          >
          > RewriteEngine On
          >
          > RewriteCond %{HTTP_HOST} !^www\.rootmode \.com
          > RewriteRule ^(.*)$ http://www\.rootmode\. com/$1 [R=
          > 301,L]
          >
          > RewriteCond %{REQUEST_FILEN AME} -f [OR]
          > RewriteCond %{REQUEST_FILEN AME} -d
          > RewriteRule ^(.*)$ $1 [L]
          >
          > RewriteCond %{REQUEST_URI} !^/index\.php
          > RewriteRule ^(.*)$ /index.php?page= $1 [L]
          >
          >
          > --
          > Edward Alfert
          > http://www.rootmode.com/
          > Multiple Domain Hosting and Reseller Hosting Plans
          > Coupon Code (Recurring $5/month Discount): newsgroup[/color]

          One of the reasons I am using .html is to give the site the look and feel of
          a static html web site. I was under the impression, that with some
          exceptions (like Google) search engines list the pages better than
          index.php?conte nt=example.
          [color=blue]
          > 2) I test to make sure the page being requested is not a file or
          > directory that exists. If it exists, then retrive the existing page.
          > This is nice in case you want to have a pages that are not part of your
          > content management system.[/color]
          If I need a stand alone page, it will probably have some php requirement, so
          I would probably just call existing pages example.php. I'm not likely to
          ever have static html files that could not be moved over to content
          management.

          Thanks for the detailed feedback. I had a look at your web site and see
          what you mean. It works very well! :-)

          Nel.


          Comment

          • Nel

            #6
            Re: .htaccess and PHP

            "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
            news:SK-dnc0eOqwUcVrd4p 2dnA@comcast.co m...[color=blue]
            > "Nel" <nelly@ne14.co. NOSPAMuk> wrote in message
            > news:DwGxc.1289 4$NK4.1841718@s tones.force9.ne t...[color=green]
            > > Just looking for some general advice on modifying the URL for php.
            > >
            > > I am using .htaccess to allow a web site to translate example.html to
            > > index.php?conte nt=example
            > > (below)
            > >
            > > ErrorDocument 404 http://www.example.com
            > > RewriteEngine on
            > > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
            > >
            > > Is there any downside to using this method. I just want to know before[/color][/color]
            I[color=blue][color=green]
            > > roll it out over several other sites.[/color]
            >
            > Other then losing the ability to use regular .html files, there's no major
            > downside I can think of. Then again, there's little upside to doing things
            > this way. If you don't need to use Apache rewrite, my advise is to not use
            > it.
            >[/color]
            As I just posted above Chung, I was under the impression that example.html
            was easier for search engines to map.

            Nel


            Comment

            • Edward Alfert

              #7
              Re: .htaccess and PHP

              "Nel" <nelly@ne14.co. NOSPAMuk> wrote in
              news:LvUxc.1316 3$NK4.1896572@s tones.force9.ne t:
              [color=blue]
              > "Edward Alfert" <ealfert@rootmo de.com> wrote in message
              > news:Xns9503CBB 689139ealfertro otmodecom@130.1 33.1.4...[color=green]
              >> "Nel" <nelly@ne14.co. NOSPAMuk> wrote in news:DwGxc.1289 4$NK4.1841718
              >> @stones.force9. net:
              >>[color=darkred]
              >> > Just looking for some general advice on modifying the URL for php.
              >> >
              >> > I am using .htaccess to allow a web site to translate example.html
              >> > to index.php?conte nt=example
              >> > (below)
              >> >
              >> > ErrorDocument 404 http://www.example.com
              >> > RewriteEngine on
              >> > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
              >> >
              >> > Is there any downside to using this method. I just want to know[/color]
              >> before I[color=darkred]
              >> > roll it out over several other sites.
              >> >
              >> > Nel.
              >> >[/color]
              >>
              >> Nel,
              >>
              >> I use something similar.
              >> 1) I redirect alias domains and domains without www. in front to my
              >> main domain using a 301 redirect which is what Google likes in order
              >> to maintain pagerank.
              >> 2) I test to make sure the page being requested is not a file or
              >> directory that exists. If it exists, then retrive the existing page.
              >> This is nice in case you want to have a pages that are not part of
              >> your content management system.
              >> 3) I don't just redirect pages with .html extensions. I redirect
              >> everything into content management system and then the cms parses the
              >> requested page and strips extensions, trailing slashes, etc.
              >>
              >>
              >>
              >> Options +SymlinksIfOwne rMatch
              >>
              >> RewriteEngine On
              >>
              >> RewriteCond %{HTTP_HOST} !^www\.rootmode \.com
              >> RewriteRule ^(.*)$ http://www\.rootmode\. com/$1 [R=
              >> 301,L]
              >>
              >> RewriteCond %{REQUEST_FILEN AME} -f [OR]
              >> RewriteCond %{REQUEST_FILEN AME} -d
              >> RewriteRule ^(.*)$ $1 [L]
              >>
              >> RewriteCond %{REQUEST_URI} !^/index\.php
              >> RewriteRule ^(.*)$ /index.php?page= $1 [L]
              >>
              >>
              >> --
              >> Edward Alfert
              >> http://www.rootmode.com/
              >> Multiple Domain Hosting and Reseller Hosting Plans
              >> Coupon Code (Recurring $5/month Discount): newsgroup[/color]
              >
              > One of the reasons I am using .html is to give the site the look and
              > feel of a static html web site. I was under the impression, that with
              > some exceptions (like Google) search engines list the pages better
              > than index.php?conte nt=example.
              >[color=green]
              > > 2) I test to make sure the page being requested is not a file or
              >> directory that exists. If it exists, then retrive the existing page.
              >> This is nice in case you want to have a pages that are not part of
              >> your content management system.[/color]
              > If I need a stand alone page, it will probably have some php
              > requirement, so I would probably just call existing pages example.php.
              > I'm not likely to ever have static html files that could not be moved
              > over to content management.
              >
              > Thanks for the detailed feedback. I had a look at your web site and
              > see what you mean. It works very well! :-)
              >
              > Nel.
              >[/color]

              Nel,

              The url is not rewritten to http://www.domain.tld/index.php?
              content=example ... it stays http://www.domain.tld/example so search
              engines do not see that it is a dynamic site. They see a static url.

              The site you looked at uses a differnt but similar technique that has
              some drawbacks (like not being able to handle trailing slashes "/").
              The code I listed is for the redesign I'm currently working on.

              The code above is only the .htaccess portion, see the index.php page
              that does other manipulation (not 100% done yet).

              <?php
              //=============== =============== ==========
              //RootMode CMS - http://cms.rootmode.com/
              //----------------------------------------

              /*************** *************** ***********
              Place the following in .htaccess in root directory
              Make sure to modify domain in first RewriteCond and RewriteRule
              *************** *************** ************
              Options +SymlinksIfOwne rMatch

              RewriteEngine On

              RewriteCond %{HTTP_HOST} !^www\.rootmode \.net
              RewriteRule ^(.*)$ http://www\.rootmode\. net/$1 [R=
              301,L]

              RewriteCond %{REQUEST_FILEN AME} -f [OR]
              RewriteCond %{REQUEST_FILEN AME} -d
              RewriteRule ^(.*)$ $1 [L]

              RewriteCond %{REQUEST_URI} !^/index\.php
              RewriteRule ^(.*)$ /index.php?page= $1 [L]
              *************** *************** ***********/

              //=============== =============== ==========
              //Set system/site specific variables
              //----------------------------------------
              $domain = $_SERVER["HTTP_HOST"]; //Can also set
              manually to "www.domain.tld "
              $path = "/var/www/html/"; //Absolute path is
              preferred but relative will work
              $dir_content = "content/";
              $ext = ".php"; //Extension of
              physical content files. Will not be displayed in address bar of
              browser.
              $page_default = "multiple-domain-hosting"; //No extension
              $redirect_defau lt = true; //true or false.
              Useful for search engine optimization if set to true along with a
              descriptive $page_default.
              $page_404 = "sitemap"; //No extension
              $dir_template = "template/";
              $page_header = "header.php "; //Extension
              required
              $page_footer = "footer.php "; //Extension
              required
              $debug = true; //true or false

              //=============== =============== ==========
              //DO NOT MODIFY BELOW THIS LINE
              //----------------------------------------

              //=============== =============== ==========
              //Send email if referred is a search engine
              //----------------------------------------

              //=============== =============== ==========
              //Read requested page from mod_rewrite rule in .htaccess
              //----------------------------------------
              $page_content = $_REQUEST["page"];
              if ($debug) { echo "(Test=User Requested Page):
              page_content=". $page_content." <br />"; };

              //=============== =============== ==========
              //If no page was specified, retrieve default page
              //----------------------------------------
              if ((!$page_conten t) && ($redirect_defa ult)) {
              $page_redirect = $page_default;} ;
              if ($debug) { echo "(Test=Redi rect Default):
              page_redirect=" .$page_redirect ."<br />"; };
              if (!$page_content ) { $page_content = $page_default; };
              if ($debug) { echo "(Test=Defa ult Page):
              page_content=". $page_content." <br />"; };

              //=============== =============== ==========
              //If exists, remove trailing "/"
              //Fix for search engines that append a "/" if the file being
              requested does not have an extension
              //Do not redirect to non-slash version because it might create a
              loop
              //----------------------------------------
              if (substr($page_c ontent,-1,1) == "/") { $page_content =
              substr($page_co ntent,0,-1); };
              if ($debug) { echo "(Test=Trai ling Slash):
              page_content=". $page_content." <br />"; };

              //=============== =============== ==========
              //If exists, remove "." or ">"
              //Fix for common problems of mangled links in emails
              //----------------------------------------
              $lastchar = substr($page_co ntent, -1, 1);
              if (($lastchar == '.') || ($lastchar == '>')) {
              $page_redirect = substr($page_co ntent, 0, -1); $page_content =
              $page_redirect; };
              if ($debug) { echo "(Test=Trai ling . or >):
              page_redirect=" .$page_redirect ."<br />"; };

              //=============== =============== ==========
              //If exists, remove extension
              //----------------------------------------
              //.htm, .php
              if (substr($page_c ontent,-4,1) == ".") {
              $page_redirect = substr($page_co ntent,0,-4); $page_content =
              $page_redirect; };
              //.html, .php3
              if (substr($page_c ontent,-5,1) == ".") {
              $page_redirect = substr($page_co ntent,0,-5); $page_content =
              $page_redirect; };
              //.shtml
              if (substr($page_c ontent,-6,1) == ".") {
              $page_redirect = substr($page_co ntent,0,-6); $page_content =
              $page_redirect; };
              if ($debug) { echo "(Test=Stri p Extension): page_redirect=" .
              $page_redirect. "<br />"; };

              //=============== =============== ==========
              //Force lower case for filename
              //----------------------------------------
              $page_content_l ower = strtolower($pag e_content);
              if ($page_content != $page_content_l ower) { $page_redirect =
              $page_content_l ower; $page_content = $page_redirect; };
              if ($debug) { echo "(Test=Lowe r Case):
              page_redirect=" .$page_redirect ."<br />"; };

              //=============== =============== ==========
              //If requested page does not exist, then redirect to 404 page
              //----------------------------------------
              $file = $path.$dir_cont ent.$page_conte nt.$ext;
              if ($debug) { echo "(Test=Phys ical Path): file=".
              $file."<br />"; };
              if (!file_exists($ file)) { $page_redirect = $page_404; };
              if ($debug) { echo "(Test=404) : page_redirect=" .
              $page_redirect. "<br />"; };

              //=============== =============== ==========
              //Determine which template (header and footer) to use
              //----------------------------------------
              if (!file_exists($ path.$dir_conte nt.$page_header )) {
              $dir_header = $dir_content; } else { $dir_header = $dir_template; };
              if ($debug) { echo "(Test=Head er Path): dir_header=".
              $dir_header."<b r />"; };
              if (!file_exists($ path.$dir_conte nt.$page_footer )) {
              $dir_footer = $dir_content; } else { $dir_footer = $dir_template; };
              if ($debug) { echo "(Test=Foot er Path): dir_footer=".
              $dir_footer."<b r />"; };

              //=============== =============== ==========
              //Display content or do a 301 redirect to correct page
              //----------------------------------------
              if (!$page_redirec t) {
              include($path.$ dir_header.$pag e_header);
              include($path.$ dir_content.$pa ge_content.$ext );
              include($path.$ dir_footer.$pag e_footer);
              } else {
              header("Locatio n: http://$domain/$page_redirect" );
              header("HTTP/1.0 301 Moved Permanently", true);
              }

              //=============== =============== ==========
              //Helpful troubleshooting info
              //----------------------------------------
              if ($debug) { echo phpinfo(); };
              //Uncomment next line to display phpinfo() even when not in
              debug mode
              //echo phpinfo();
              ?>

              --
              Edward Alfert

              Multiple Domain Hosting and Reseller Hosting Plans
              Coupon Code (Recurring $5/month Discount): newsgroup

              Comment

              • Virgil Green

                #8
                Re: .htaccess and PHP

                "Nel" <nelly@ne14.co. NOSPAMuk> wrote in message
                news:LvUxc.1316 3$NK4.1896572@s tones.force9.ne t...[color=blue]
                > "Edward Alfert" <ealfert@rootmo de.com> wrote in message
                > news:Xns9503CBB 689139ealfertro otmodecom@130.1 33.1.4...[color=green]
                > > "Nel" <nelly@ne14.co. NOSPAMuk> wrote in news:DwGxc.1289 4$NK4.1841718
                > > @stones.force9. net:
                > >[color=darkred]
                > > > Just looking for some general advice on modifying the URL for php.
                > > >
                > > > I am using .htaccess to allow a web site to translate example.html to
                > > > index.php?conte nt=example
                > > > (below)
                > > >
                > > > ErrorDocument 404 http://www.example.com
                > > > RewriteEngine on
                > > > RewriteRule ^/?(.*).html$ /index.php?conte nt=$1 [L]
                > > >
                > > > Is there any downside to using this method. I just want to know[/color]
                > > before I[color=darkred]
                > > > roll it out over several other sites.
                > > >
                > > > Nel.
                > > >[/color]
                > >
                > > Nel,
                > >
                > > I use something similar.
                > > 1) I redirect alias domains and domains without www. in front to my main
                > > domain using a 301 redirect which is what Google likes in order to
                > > maintain pagerank.
                > > 2) I test to make sure the page being requested is not a file or
                > > directory that exists. If it exists, then retrive the existing page.
                > > This is nice in case you want to have a pages that are not part of your
                > > content management system.
                > > 3) I don't just redirect pages with .html extensions. I redirect
                > > everything into content management system and then the cms parses the
                > > requested page and strips extensions, trailing slashes, etc.
                > >
                > >
                > >
                > > Options +SymlinksIfOwne rMatch
                > >
                > > RewriteEngine On
                > >
                > > RewriteCond %{HTTP_HOST} !^www\.rootmode \.com
                > > RewriteRule ^(.*)$ http://www\.rootmode\. com/$1 [R=
                > > 301,L]
                > >
                > > RewriteCond %{REQUEST_FILEN AME} -f [OR]
                > > RewriteCond %{REQUEST_FILEN AME} -d
                > > RewriteRule ^(.*)$ $1 [L]
                > >
                > > RewriteCond %{REQUEST_URI} !^/index\.php
                > > RewriteRule ^(.*)$ /index.php?page= $1 [L]
                > >
                > >
                > > --
                > > Edward Alfert
                > > http://www.rootmode.com/
                > > Multiple Domain Hosting and Reseller Hosting Plans
                > > Coupon Code (Recurring $5/month Discount): newsgroup[/color]
                >
                > One of the reasons I am using .html is to give the site the look and feel[/color]
                of[color=blue]
                > a static html web site. I was under the impression, that with some
                > exceptions (like Google) search engines list the pages better than
                > index.php?conte nt=example.
                >[/color]

                If you'd like to see a rather extensive use of rewrite to convert dynamic
                URLs to apparently static URLs, you might want to visit www.nukecops.com and
                take a look at the GoogleTap proposal that is fairly popular with phpNuke
                users. I've looked at for educational purposes but have not chosen to use
                it. It handles the re-write on incoming transactiosn and also rewrites
                outbound html.

                - Virgil


                Comment

                Working...