URL use "/" instead of "?" to send variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulephp
    New Member
    • Sep 2009
    • 59

    #1

    URL use "/" instead of "?" to send variables

    Hi,
    In normal php, We use ?id=123 to send variable through URL.

    So, the URL would be www.example.com/page.php?id=123 and then we use $_REQUEST['id'] to receive the variable on another page.

    But my question is, if I use "/" instead of "?" (Ex: www.example.com/page.php/id/123), then how to receive the "id" on page.php.

    $_REQUEST['id'] doesn't works in this case.

    Thank you in advance
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    $_REQUEST['id'] doesn't works in this case.
    Does the browser still go to page.php in this case?
    I doubt it.

    Comment

    • rahulephp
      New Member
      • Sep 2009
      • 59

      #3
      Not sure.
      But i have seen these kind of URL's in lots of websites and just wondering if it'll be possible in any ways.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        i think mod_rewrite allows this.
        Don't know much about it but Apache only

        Comment

        • rahulephp
          New Member
          • Sep 2009
          • 59

          #5
          I tested the above code and it shows 404 error page.
          Yes, I need to modify .htaccess file

          Can anyone please assist me how to modify .htaccess file to remove the "?"

          I'll be thankful to you

          Comment

          • ronsplinter
            New Member
            • Jun 2010
            • 1

            #6
            Originally posted by rahulephp
            Hi,
            In normal php, We use ?id=123 to send variable through URL.

            So, the URL would be www.example.com/page.php?id=123 and then we use $_REQUEST['id'] to receive the variable on another page.

            But my question is, if I use "/" instead of "?" (Ex: www.example.com/page.php/id/123), then how to receive the "id" on page.php.

            $_REQUEST['id'] doesn't works in this case.

            Thank you in advance
            this is done by the rewrite module of apache and this handles regular expressions. You have to put a rule like this in your .htaccess file on the root of your website:

            RewriteRule ^product/([0-9]+)$ /page.php?id=$1

            ^ means the start of the url after www.example.com/
            $ means the end of the page.

            www.example.com/product/123 will be converted by the server to: www.example.com/page.php?id=123

            In PHP you use the normal $_GET['id'] variable. The rewrite module must be enabled by apache... This is mostly used to make the url format independent of the serverside scripting language so the .php in the url is not logical. Thats why i changed it to product/ . The .htaccess starts with

            RewriteEngine On
            Options +FollowSymLinks
            RewriteBase /
            Here all the rewrite rules..
            ...
            ...

            Hope this will help you on your way.

            Comment

            • rahulephp
              New Member
              • Sep 2009
              • 59

              #7
              Originally posted by ronsplinter
              this is done by the rewrite module of apache and this handles regular expressions. You have to put a rule like this in your .htaccess file on the root of your website:

              RewriteRule ^product/([0-9]+)$ /page.php?id=$1

              ^ means the start of the url after www.example.com/
              $ means the end of the page.

              www.example.com/product/123 will be converted by the server to: www.example.com/page.php?id=123

              In PHP you use the normal $_GET['id'] variable. The rewrite module must be enabled by apache... This is mostly used to make the url format independent of the serverside scripting language so the .php in the url is not logical. Thats why i changed it to product/ . The .htaccess starts with

              RewriteEngine On
              Options +FollowSymLinks
              RewriteBase /
              Here all the rewrite rules..
              ...
              ...

              Hope this will help you on your way.
              Thanks ronsplinter,
              This is one of the best answer i was searching for.

              Comment

              • HaLo2FrEeEk
                Contributor
                • Feb 2007
                • 404

                #8
                There are nearly limitless things you can do with mod_rewrite under Apache. For example, I have a click counter on my site that keeps track of the number of clicks to each linked news post, using a link id. Originally the URL looked like this:



                I wanted it to be more search engine-friendly, so I rewrote it to this:



                It still goes to the original click.ph URL, but the user doesn't know that, and Google indexes thestatic-looking link a lot more easily.

                I'm in the process or rewriting my entire site to use mod_rewritten URLs, to improve SEO.

                Comment

                • rahulephp
                  New Member
                  • Sep 2009
                  • 59

                  #9
                  Originally posted by HaLo2FrEeEk
                  There are nearly limitless things you can do with mod_rewrite under Apache. For example, I have a click counter on my site that keeps track of the number of clicks to each linked news post, using a link id. Originally the URL looked like this:



                  I wanted it to be more search engine-friendly, so I rewrote it to this:



                  It still goes to the original click.ph URL, but the user doesn't know that, and Google indexes thestatic-looking link a lot more easily.

                  I'm in the process or rewriting my entire site to use mod_rewritten URLs, to improve SEO.
                  Hey HaLo2FrEeEk,
                  if you are going to rewrite your entire site to use mod_rewritten URLs and if your website has ranked in SERP for good keywords then think it once. The URLs of all you website pages will be changed and the URLS will suppose to be new pages. And in that case you'll lost your previous ranking.

                  Comment

                  • rahulephp
                    New Member
                    • Sep 2009
                    • 59

                    #10
                    I'm thankful for your good response on this but now i want to close the topic here.
                    Thanks once again

                    Comment

                    • HaLo2FrEeEk
                      Contributor
                      • Feb 2007
                      • 404

                      #11
                      My site isn't ranked, at least not highly. You'll very rarely come up with my website when searching Google, unless you search for the website title itself. That's why I plan to rewrite it, to make it easier for Google to index the URLs.

                      Comment

                      • rahulephp
                        New Member
                        • Sep 2009
                        • 59

                        #12
                        Originally posted by HaLo2FrEeEk
                        My site isn't ranked, at least not highly. You'll very rarely come up with my website when searching Google, unless you search for the website title itself. That's why I plan to rewrite it, to make it easier for Google to index the URLs.
                        You are right, you should use mod_rewritten URLs for better performance. You can also send me URL of your website as PM so that i would have a look on that.

                        Comment

                        Working...