how to hide file name (.php)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuananh87vn
    New Member
    • Sep 2007
    • 63

    how to hide file name (.php)

    hi,
    I see that it's easy to create a navigation like index.php?id=1, index.php?id=2. .. then you can hide the current part you are in. But I need a further step (as I've seen many), hope it's easy enough, that how can hide the file name index.php so that the above links become simple "?id=1, ?id=2... if so, that'd be great :)
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Ok lets use
    [php]
    www.example.com/index.php?id=1
    [/php]
    for the demonstration url.

    If you want to hide the 'index.php' name just cut it out

    i.e:
    [php]
    www.example.com/?id=1
    [/php]
    Remeber to keep the forward slash - / - in there.

    Note, this will only work for index pages as index pages are the default catch pages for browsers.

    [php]
    www.example.com/some_directory/index.php?id=1
    //becomes
    www.example.com/some_directory/?id=1
    [/php]
    So on and so forth.

    Hope this helped :)

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by tuananh87vn
      hi,
      I see that it's easy to create a navigation like index.php?id=1, index.php?id=2. .. then you can hide the current part you are in. But I need a further step (as I've seen many), hope it's easy enough, that how can hide the file name index.php so that the above links become simple "?id=1, ?id=2... if so, that'd be great :)
      Hi,

      There is a realy neat and easy way to do this without loading loads and loads of directories that you don't wan't. The directories don't really hide where the visitor is they simply move it.

      If you check out www.just10northeast.org.uk you will see the following outline in operation.

      Every link calls a JavaScript function via the onclick method. This is instead of a normal href property.

      The function takes a number of parameters, the first is the main control parameter which is used by the function to determine where the required page is defined. The second is anything the page definition requires and the third is the ID of a div on the main page.

      Then using the first parameter the relevant page is determined and this is accessed using the XMLHTTP object with the secind paratmter in the query string which returns the definition and the JS function then writes this return to the div whose ID was supplied as the third parameter.

      So this uses AJAX to solve the problem and it means that the URL is always the same and the site never reloads a full page but only part of it making it quite swift.

      The key term for research here is AJAX.

      If you want some more specific help then let me know and I'll happily look over any code for you and even help with a bit of development.

      Cheers
      nathj

      Comment

      • helimeef
        New Member
        • Sep 2007
        • 77

        #4
        Originally posted by tuananh87vn
        hi,
        I see that it's easy to create a navigation like index.php?id=1, index.php?id=2. .. then you can hide the current part you are in. But I need a further step (as I've seen many), hope it's easy enough, that how can hide the file name index.php so that the above links become simple "?id=1, ?id=2... if so, that'd be great :)
        Along with the first answer, you can also use a neat trick called mod_rewrite. It's a little bit advanced, but if you want to try it you can go to http://www.workingwith .me.uk/articles/scripting/mod_rewrite
        And make sure that in httpd.conf (apache config file) AllowOverride is set to "All", so the line containing AllowOverride (without a # sign before it) should say "AllowOverr ide All".
        Hope that helped.

        Comment

        • tuananh87vn
          New Member
          • Sep 2007
          • 63

          #5
          thanks for all, you guys are so kind :)

          if it's to work only then one of the above is enough of course, but I want to discover something new so the method involved AJAX would be great. if u don't mind, so tell me how?

          again, thanks for your great help :D

          Comment

          • realin
            Contributor
            • Feb 2007
            • 254

            #6
            why not try URL rewriting, if i understood your problem correctly.
            Also you can edit .htaccess to hide the technology you write the files in. You can rename your file as index.pl and then some editing in .htaccess will treat .pl as .php , resulting files will compile as php files

            If i misunderstood you then i am sorry

            Cheers !!

            Comment

            • nathj
              Recognized Expert Contributor
              • May 2007
              • 937

              #7
              Originally posted by tuananh87vn
              thanks for all, you guys are so kind :)

              if it's to work only then one of the above is enough of course, but I want to discover something new so the method involved AJAX would be great. if u don't mind, so tell me how?

              again, thanks for your great help :D
              Hi,

              It's always good to learn something new so I've no porblem explaining the way I achieved this.

              First of all you need a few JavaScript Functions.
              Code:
              /*------------------------------------------------------------
              	GENERAL AJAX FUNCTIONS
              ------------------------------------------------------------*/	
              function writeResponse(poXMLHTTP, pcEmelentID) 
              { 	   
              	if (poXMLHTTP.readyState==4 || poXMLHTTP.readyState=="complete")
              	{ 
              		if (poXMLHTTP.status == 200)
              		{
              			document.getElementById(pcEmelentID).innerHTML=poXMLHTTP.responseText ;  
              		}
              	} 
              } 
              
              function GetXmlHttpObject()
              { 
              	if (window.XMLHttpRequest)
              	{
              		loXMLHTTP=new XMLHttpRequest() ;
              	}
              	else if (window.ActiveXObject)
              	{
              		loXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP") ;
              	}	 
              	if (loXMLHTTP==null)
              	{
              		alert ("Browser does not support HTTP Request")	;
              		 return	;
              	}
              	return loXMLHTTP ;
              } 
              
              function submitXMLHTTP(poXMLHTTP, pcIDToWriteTo, pcOpenMethod, pcURL, plOpen, pcSendData)
              {																						
              	poXMLHTTP.onreadystatechange = function() 
              	{ 
              		// use of ghost function enables the system to execute each item in the correct order
              		writeResponse(poXMLHTTP, pcIDToWriteTo); 
              	};
              	poXMLHTTP.open(pcOpenMethod,pcURL,plOpen) ;
              	poXMLHTTP.send(pcSendData) ; 
              } 
              
              
              
              /*------------------------------------------------------------
              	MENU/DISPLAY HANDLING FUNCTIONS
              ------------------------------------------------------------*/	
              function menuHandler(pnType, pnMenuID, pcWriteToID)
              {												  
              	if(trim(pcWriteToID) == "")
              	{
              		pcWriteToID = "mainContent" ; // default value
              	}
              	
              	// instantiate the XMLHTTP object
              	loXMLHTTP = GetXmlHttpObject() ;
              	
              	lcBaseURL = "pagedefinition/" ; // this is the directory where all the page definition files are stored
              
              	// determine which URL we are loading to handle this menu selection
              	switch(pnType)
              	{
              		case 1: // Main content  
              			lcURL = lcBaseURL + "maincontent.php?page=" + pnMenuID ; // I then use the ID from the query string to select data out of a table. This may not be necessary if each page is static 
              			break ;
              		case 4: // downloads 				  
              			lcURL = lcBaseURL + "download.php" ; // not all pages need this query string but the call to this function would still requie the middle parameter
              			break ;
              	} 
              	/* this is just a sample, I have about 9 different definitions in here. Most pages however use the first case as the page is fully definied in my database. As this is a switch statement you can add as many as you like*/
              	
              	submitXMLHTTP(loXMLHTTP, pcWriteToID, "GET", lcURL, true, null) ;
              }
              Then in you html file - index.html you would have a section, I use <div> with the ID that is submitted as the third parameter, in my case this is mainContent

              An example link would look like:
              [html]
              <a title="Link tool tip" onmousever="thi s.style.cursor= 'pointer';" onclick="menHan dler(1, 1, 'mainContent'); ">Link Text</a>
              [/html]


              The advantage of this system is that because the output section is a parameter you can use to retreive the definition of any section on your page. Also because the page definition file is located based on and paramter this is fully customisable. The middle parater can then be used to get an exact record from a database and so if you have a database you can use one file to define multiple pages by having the pages defined in the database.

              Well there you have it. I've given away some code for you so I hope this will help you ti understand what is going on.

              Have a play around with this and if you have any further questions just let me know.

              Cheers
              nathj

              Comment

              • helimeef
                New Member
                • Sep 2007
                • 77

                #8
                Originally posted by realin
                why not try URL rewriting, if i understood your problem correctly.
                Also you can edit .htaccess to hide the technology you write the files in. You can rename your file as index.pl and then some editing in .htaccess will treat .pl as .php , resulting files will compile as php files

                If i misunderstood you then i am sorry

                Cheers !!
                I already said that, mod_rewrite.

                Comment

                • tuananh87vn
                  New Member
                  • Sep 2007
                  • 63

                  #9
                  hi,
                  I did have some tries and find mod_rewrite definitely useful, but I still wonders:

                  if the browser understands, say, www.site.com/page/download as www.site.com/index.php?id=5 so how can we still use the value $_GET['id']?, for which we have another purpose?

                  the second question, we change site.com/index.php?page= download into site.com/download, so how about site.com/index.php?page= download&action =get, for example?

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    To answer your second question
                    using the same technique you used for mod_rewrite previously, make the url: www.site.com/download/action/get

                    Not sure about your first question

                    what are these 'other purposes' you spoke of?

                    Comment

                    • amagondes
                      New Member
                      • Dec 2007
                      • 3

                      #11
                      Hi, if you are looking for something like this where the id is dynamically generated at runtime:

                      site.com/page/download/action/get/id/4875
                      which would translate to:
                      site.com/index.php?page= download&action =get&id=4875

                      To do this try something like this in your htaccess:
                      Code:
                      RewriteEngine on
                      RewriteRule ^page/download/action/get/id/(.*) index.php?page=download&action=get&id=$1
                      Then in your php script that generates the link just do something like:

                      Code:
                      <a href="page/download/action/get/id/<?= $some_id ?>">Download</a>
                      From here you can simply access the url segments from the $_GET array
                      Code:
                      <?= $_GET['action'] ?> or <?= $_GET['id'] ?>
                      One last thing. If you need totally dynamic urls just write a rule to point everthing but images, js files, css etc to index.php and then you can split the url on the forward slash and access the segments that way.

                      I hope that helps you out

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by amagondes
                        [code
                        RewriteEngine on
                        RewriteRule ^page/download/action/get/id/(.*) index.php?page= download&action =get&id=$1
                        [/code]
                        I believe, if you wanted the url's appearence to stay the same, you'd have to add [L] to the end of the Rule

                        i.e.
                        [code=apache]
                        RewriteEngine on
                        RewriteRule ^page/download/action/get/id/(.*)
                        index.php?page= download&action =get&id=$1 [L]
                        [/code]

                        :)

                        Comment

                        • amagondes
                          New Member
                          • Dec 2007
                          • 3

                          #13
                          Originally posted by markusn00b
                          I believe, if you wanted the url's appearence to stay the same, you'd have to add [L] to the end of the Rule

                          i.e.
                          [code=apache]
                          RewriteEngine on
                          RewriteRule ^page/download/action/get/id/(.*)
                          index.php?page= download&action =get&id=$1 [L]
                          [/code]

                          :)
                          That is just to let apache know that it is the last rule. The code works fine. I may be missing something, could you explain what you mean by "if you wanted the url's appearence to stay the same".
                          Thanks

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            To keep the url from changing from www.site.com/action/get/id/3223 to www.site.com/?action=get&id= 3223

                            The best example is myspace
                            www.myspace.com/user_name this is how the url looks but essentially its www.myspace.com/?user=user_name


                            Sorry.. im tired. hha. :)

                            Comment

                            • amagondes
                              New Member
                              • Dec 2007
                              • 3

                              #15
                              Originally posted by markusn00b
                              To keep the url from changing from www.site.com/action/get/id/3223 to www.site.com/?action=get&id= 3223

                              The best example is myspace
                              www.myspace.com/user_name this is how the url looks but essentially its www.myspace.com/?user=user_name


                              Sorry.. im tired. hha. :)
                              Thanks markusn00b, I'm not an expert with mod rewrite :)

                              Comment

                              Working...