Change request URL

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

    Change request URL

    Hey, I want to standardize the URL that users use to get to my site.
    For example, I want to change:



    to:


    and I want to change:



    to:


    Basically I want to make sure every page has the www component of the
    url, and I want to add the index.php to the home page url if it's not
    present. How can I do this either from PHP or using a different method?
  • Jerry Stuckle

    #2
    Re: Change request URL

    bgold12 wrote:
    Hey, I want to standardize the URL that users use to get to my site.
    For example, I want to change:
    >

    >
    to:

    >
    and I want to change:
    >

    >
    to:

    >
    Basically I want to make sure every page has the www component of the
    url, and I want to add the index.php to the home page url if it's not
    present. How can I do this either from PHP or using a different method?

    You can't do it from PHP or any other server-side language (at least not
    easily) - that's way too late. You need to do it in your server
    configuration. Try the appropriate web server newsgroup, i.e.
    alt.apache.conf iguration.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • J.O. Aho

      #3
      Re: Change request URL

      Jerry Stuckle wrote:
      bgold12 wrote:
      >Hey, I want to standardize the URL that users use to get to my site.
      >For example, I want to change:
      >>
      >http://example.com/
      >>
      >to:
      >http://www.example.com/index.php
      >>
      >and I want to change:
      >>
      >http://example.com/page.php
      >>
      >to:
      >http://www.example.com/page.php
      >>
      >Basically I want to make sure every page has the www component of the
      >url, and I want to add the index.php to the home page url if it's not
      >present. How can I do this either from PHP or using a different method?
      >
      >
      You can't do it from PHP or any other server-side language (at least not
      easily) - that's way too late. You need to do it in your server
      configuration. Try the appropriate web server newsgroup, i.e.
      alt.apache.conf iguration.
      >
      Using mod_rewrite, the Apache will use header redirections, you can do the
      same in php too, using $_SERVER values to check if the user made the request
      correctly according to the OP post and then in those cases it's not correct,
      use header() to redirect to the url which they should have written from the
      begining. Don't forget to take care of $_GET/$_POST values and include those
      to the header too.

      I can say I personally against redirection for normal request, just look at
      php.net, do a request for php,net/header and you end up somewhere else, in my
      case always at a slow performing server, many times it goes faster to open a
      new tab and write uk2.php.net/header

      --

      //Aho

      Comment

      • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

        #4
        Re: Change request URL

        bgold12 escribió:
        Hey, I want to standardize the URL that users use to get to my site.
        For example, I want to change:
        >

        >
        to:

        >
        and I want to change:
        >

        >
        to:

        >
        Basically I want to make sure every page has the www component of the
        url, and I want to add the index.php to the home page url if it's not
        present. How can I do this either from PHP or using a different method?
        In PHP, you just need to check the URL in every page and perform an HTTP
        redirection if it doesn't match your standard. You can lookup for
        available variables with print_r($_SERVE R) and then generate a header:

        header('Locatio n: http://www.example.com/...........', true, 301);

        The 301 HTTP status code means "Moved Permanently". I suppose you have a
        common 'settings.php' or whatever where you can place the call to these
        checks. Don't forget the query string in the URL (thes post parameters
        will be lost though).

        However, it's normally easier to do all this with the web server itself.
        Apache, for instance, allows you to specify the www check with this syntax:

        # Not tested!
        RewriteCond %{HTTP_HOST} !^www\.example\ .com$ [NC]
        RewriteRule ^.*$ http://www.example.com/$0 [R=301,QSA,L]

        Note this is *not* PHP. I suppose other web servers have similar features.


        --
        -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
        -- Mi sitio sobre programación web: http://bits.demogracia.com
        -- Mi web de humor al baño María: http://www.demogracia.com
        --

        Comment

        • rf

          #5
          Re: Change request URL


          "J.O. Aho" <user@example.n etwrote in message
          news:6o4jb5F1ha f0U1@mid.indivi dual.net...
          Jerry Stuckle wrote:
          >bgold12 wrote:
          >>Hey, I want to standardize the URL that users use to get to my site.
          >>For example, I want to change:
          >>>
          >>http://example.com/
          >>>
          >>to:
          >>http://www.example.com/index.php
          >>>
          >>and I want to change:
          >>>
          >>http://example.com/page.php
          >>>
          >>to:
          >>http://www.example.com/page.php
          >>>
          >>Basically I want to make sure every page has the www component of the
          >>url, and I want to add the index.php to the home page url if it's not
          Using mod_rewrite, the Apache will use header redirections,
          mod_rewrite does not use header redirections, it rewrites the existing
          request URL before the server gets a look at it.
          you can do the
          same in php too, using $_SERVER values to check if the user made the
          request
          correctly according to the OP post and then in those cases it's not
          correct,
          use header() to redirect to the url which they should have written from
          the
          begining. Don't forget to take care of $_GET/$_POST values and include
          those
          to the header too.
          Nope.

          www happens (or should happen) at the DNS level, way before apache gets a
          look in and most certainly way before PHP has a go.

          This is *not* a PHP issue, it is about domain name servers plus a bit of
          HTTP server setup. The former to ensure there is a CNAME record to alias
          www.example.com to example.com and the latter to ensure index.php is the
          default file to be served when there is no file mentioned in the URL.




          Comment

          • Geoff Berrow

            #6
            Re: Change request URL

            Message-ID: <UhaTk.14938$sc 2.4392@news-server.bigpond. net.aufrom rf
            contained the following:
            >www happens (or should happen) at the DNS level, way before apache gets a
            >look in and most certainly way before PHP has a go.
            >
            >This is *not* a PHP issue, it is about domain name servers plus a bit of
            >HTTP server setup. The former to ensure there is a CNAME record to alias
            >www.example.com to example.com and the latter to ensure index.php is the
            >default file to be served when there is no file mentioned in the URL.
            When I set up domains on my VPS with cpanel/WHM it's automatic. Quite a
            handy feature.
            --
            Geoff Berrow 011000100110110 0010000000110
            001101101011011 001000110111101 100111001011
            100110001101101 111001011100111 010101101011
            http://slipperyhill.co.uk - http://4theweb.co.uk

            Comment

            • J.O. Aho

              #7
              Re: Change request URL

              rf wrote:
              "J.O. Aho" <user@example.n etwrote in message
              news:6o4jb5F1ha f0U1@mid.indivi dual.net...
              >Jerry Stuckle wrote:
              >>bgold12 wrote:
              >>>Hey, I want to standardize the URL that users use to get to my site.
              >>>For example, I want to change:
              >>>>
              >>>http://example.com/
              >>>>
              >>>to:
              >>>http://www.example.com/index.php
              >>>>
              >>>and I want to change:
              >>>>
              >>>http://example.com/page.php
              >>>>
              >>>to:
              >>>http://www.example.com/page.php
              >>>>
              >>>Basically I want to make sure every page has the www component of the
              >>>url, and I want to add the index.php to the home page url if it's not
              >
              >Using mod_rewrite, the Apache will use header redirections,
              >
              mod_rewrite does not use header redirections, it rewrites the existing
              request URL before the server gets a look at it.
              From Apache.org about mod_rewrite:

              This module operates on the full URLs (including the path-info part) both in
              per-server context (httpd.conf) and per-directory context (.htaccess) and can
              even generate query-string parts on result. The rewritten result can lead to
              internal sub-processing, external request redirection or even to an internal
              proxy throughput.

              Surely you don't have to redirect with mod_rewrite, but I was advocating for a
              similarity.

              >you can do the
              >same in php too, using $_SERVER values to check if the user made the
              >request
              >correctly according to the OP post and then in those cases it's not
              >correct,
              >use header() to redirect to the url which they should have written from
              >the
              >begining. Don't forget to take care of $_GET/$_POST values and include
              >those
              >to the header too.
              www happens (or should happen) at the DNS level, way before apache gets a
              look in and most certainly way before PHP has a go.
              This is *not* a PHP issue, it is about domain name servers plus a bit of
              HTTP server setup. The former to ensure there is a CNAME record to alias
              www.example.com to example.com and the latter to ensure index.php is the
              default file to be served when there is no file mentioned in the URL.
              I'm not going into what the OP should have done, but suggesting how he can use
              PHP to do the whole thing.


              --

              //Aho

              Comment

              • Jerry Stuckle

                #8
                Re: Change request URL

                J.O. Aho wrote:
                rf wrote:
                >"J.O. Aho" <user@example.n etwrote in message
                >news:6o4jb5F1h af0U1@mid.indiv idual.net...
                >>Jerry Stuckle wrote:
                >>>bgold12 wrote:
                >>>>Hey, I want to standardize the URL that users use to get to my site.
                >>>>For example, I want to change:
                >>>>>
                >>>>http://example.com/
                >>>>>
                >>>>to:
                >>>>http://www.example.com/index.php
                >>>>>
                >>>>and I want to change:
                >>>>>
                >>>>http://example.com/page.php
                >>>>>
                >>>>to:
                >>>>http://www.example.com/page.php
                >>>>>
                >>>>Basically I want to make sure every page has the www component of the
                >>>>url, and I want to add the index.php to the home page url if it's not
                >>Using mod_rewrite, the Apache will use header redirections,
                >mod_rewrite does not use header redirections, it rewrites the existing
                >request URL before the server gets a look at it.
                >
                From Apache.org about mod_rewrite:
                >
                This module operates on the full URLs (including the path-info part) both in
                per-server context (httpd.conf) and per-directory context (.htaccess) and can
                even generate query-string parts on result. The rewritten result can lead to
                internal sub-processing, external request redirection or even to an internal
                proxy throughput.
                >
                Surely you don't have to redirect with mod_rewrite, but I was advocating for a
                similarity.
                >
                It's the most efficient way to do it.
                >
                >>you can do the
                >>same in php too, using $_SERVER values to check if the user made the
                >>request
                >>correctly according to the OP post and then in those cases it's not
                >>correct,
                >>use header() to redirect to the url which they should have written from
                >>the
                >>begining. Don't forget to take care of $_GET/$_POST values and include
                >>those
                >>to the header too.
                >
                >www happens (or should happen) at the DNS level, way before apache gets a
                >look in and most certainly way before PHP has a go.
                >This is *not* a PHP issue, it is about domain name servers plus a bit of
                >HTTP server setup. The former to ensure there is a CNAME record to alias
                >www.example.com to example.com and the latter to ensure index.php is the
                >default file to be served when there is no file mentioned in the URL.
                >
                I'm not going into what the OP should have done, but suggesting how he can use
                PHP to do the whole thing.
                >
                >
                mod_rewrite is much more efficient to do it than a header() redirect.
                It's easier, also.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • Tim Greer

                  #9
                  Re: Change request URL

                  "Álvaro G. Vicario" wrote:
                  RewriteCond %{HTTP_HOST} !^www\.example\ .com$ [NC]
                  RewriteRule ^.*$ http://www.example.com/$0 [R=301,QSA,L]
                  I know you said this wasn't tested, but it was close. The conditional
                  is correct, but the rule used $0 (there is no $0, it starts at $1), and
                  there was nothing captured for $1, unless you use ^(.*)$ (or something
                  similar). The example should work now, but yeah, not PHP.
                  --
                  Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
                  Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
                  and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
                  Industry's most experienced staff! -- Web Hosting With Muscle!

                  Comment

                  • J.O. Aho

                    #10
                    Re: Change request URL

                    Jerry Stuckle wrote:
                    mod_rewrite is much more efficient to do it than a header() redirect.
                    It's easier, also.
                    I do agree on that, but even better if you can be without both.



                    --

                    //Aho

                    Comment

                    Working...