By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

how to hide file name (.php)

P: 63
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 :)
Dec 26 '07 #1
Share this Question
Share on Google+
14 Replies


Markus
Expert 5K+
P: 6,050
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 :)
Dec 26 '07 #2

nathj
Expert 100+
P: 938
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
Dec 26 '07 #3

helimeef
P: 77
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 "AllowOverride All".
Hope that helped.
Dec 26 '07 #4

P: 63
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
Dec 27 '07 #5

realin
100+
P: 254
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 !!
Dec 27 '07 #6

nathj
Expert 100+
P: 938
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*------------------------------------------------------------
  3.     GENERAL AJAX FUNCTIONS
  4. ------------------------------------------------------------*/    
  5. function writeResponse(poXMLHTTP, pcEmelentID) 
  6. {        
  7.     if (poXMLHTTP.readyState==4 || poXMLHTTP.readyState=="complete")
  8.     { 
  9.         if (poXMLHTTP.status == 200)
  10.         {
  11.             document.getElementById(pcEmelentID).innerHTML=poXMLHTTP.responseText ;  
  12.         }
  13.     } 
  14.  
  15. function GetXmlHttpObject()
  16.     if (window.XMLHttpRequest)
  17.     {
  18.         loXMLHTTP=new XMLHttpRequest() ;
  19.     }
  20.     else if (window.ActiveXObject)
  21.     {
  22.         loXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP") ;
  23.     }     
  24.     if (loXMLHTTP==null)
  25.     {
  26.         alert ("Browser does not support HTTP Request")    ;
  27.          return    ;
  28.     }
  29.     return loXMLHTTP ;
  30.  
  31. function submitXMLHTTP(poXMLHTTP, pcIDToWriteTo, pcOpenMethod, pcURL, plOpen, pcSendData)
  32. {                                                                                        
  33.     poXMLHTTP.onreadystatechange = function() 
  34.     { 
  35.         // use of ghost function enables the system to execute each item in the correct order
  36.         writeResponse(poXMLHTTP, pcIDToWriteTo); 
  37.     };
  38.     poXMLHTTP.open(pcOpenMethod,pcURL,plOpen) ;
  39.     poXMLHTTP.send(pcSendData) ; 
  40.  
  41.  
  42.  
  43. /*------------------------------------------------------------
  44.     MENU/DISPLAY HANDLING FUNCTIONS
  45. ------------------------------------------------------------*/    
  46. function menuHandler(pnType, pnMenuID, pcWriteToID)
  47. {                                                  
  48.     if(trim(pcWriteToID) == "")
  49.     {
  50.         pcWriteToID = "mainContent" ; // default value
  51.     }
  52.  
  53.     // instantiate the XMLHTTP object
  54.     loXMLHTTP = GetXmlHttpObject() ;
  55.  
  56.     lcBaseURL = "pagedefinition/" ; // this is the directory where all the page definition files are stored
  57.  
  58.     // determine which URL we are loading to handle this menu selection
  59.     switch(pnType)
  60.     {
  61.         case 1: // Main content  
  62.             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 
  63.             break ;
  64.         case 4: // downloads                   
  65.             lcURL = lcBaseURL + "download.php" ; // not all pages need this query string but the call to this function would still requie the middle parameter
  66.             break ;
  67.     } 
  68.     /* 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*/
  69.  
  70.     submitXMLHTTP(loXMLHTTP, pcWriteToID, "GET", lcURL, true, null) ;
  71.  
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="this.style.cursor='pointer';" onclick="menHandler(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
Dec 27 '07 #7

helimeef
P: 77
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.
Dec 27 '07 #8

P: 63
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?
Dec 28 '07 #9

Markus
Expert 5K+
P: 6,050
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?
Dec 28 '07 #10

P: 3
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:
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine on
  2. RewriteRule ^page/download/action/get/id/(.*) index.php?page=download&action=get&id=$1
  3.  
Then in your php script that generates the link just do something like:

Expand|Select|Wrap|Line Numbers
  1. <a href="page/download/action/get/id/<?= $some_id ?>">Download</a>
  2.  
From here you can simply access the url segments from the $_GET array
Expand|Select|Wrap|Line Numbers
  1. <?= $_GET['action'] ?> or <?= $_GET['id'] ?>
  2.  
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
Dec 28 '07 #11

Markus
Expert 5K+
P: 6,050
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.
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine on
  2. RewriteRule ^page/download/action/get/id/(.*)
  3. index.php?page=download&action=get&id=$1 [L]
  4.  
:)
Dec 28 '07 #12

P: 3
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.
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine on
  2. RewriteRule ^page/download/action/get/id/(.*)
  3. index.php?page=download&action=get&id=$1 [L]
  4.  
:)
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
Dec 28 '07 #13

Markus
Expert 5K+
P: 6,050
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. :)
Dec 28 '07 #14

P: 3
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 :)
Dec 28 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.