Home » Php » page url problem – PHP

page url problem – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

hii guys !!

I have a website where in i was listing all the mobiles in a page like

1.nokia 3310
2.motorola p150
3. ..

so each mobile has description page . so i was passing the model(3310) and brand(nokia) in url to a page (mobile_desc,php) where i was getting the $_GET variables and querying it .

1. now client wants some changes like the model and brand should not go through url, so how do i need to pass the model and brand to next page so that i can use it .

2. and he wants pages like http://xyz.com/nokia3310 and stuff instead of http://xyz.com/mobile_desc.php so that google can recognize it .

A(Answer):

1. $_POST variable? $_SESSION variable? $_POST for forms, but I would look into $_SESSION and maybe some AJAX to store it in the session when a user clicks something.
2. The most common way is using .htaccess mod_rewrite, but you will need to think of how your real URL relates to what you want it to look like.

A(Answer):

say i take them in a form and list them .but whn i click on nokia 3110 how will $_POST in next page know that the nokia 3110 was clicked? i had this doubt only!

A(Answer):

You can submit a form using Javascript and onChange or onClick.

A(Answer):

i think you got ma point wrong!

i am like displaying a list of all mobiles in a page . like 10-20 mobiles with pictures of the mobile . when the user clicks on the picture of the mobile it takes him to description of the mobile . i was making the url of the image to next page like this in a while loop of a mysql query

$myresult .= "<a href='camera_rating.php?p1=". $row['property1'] . "&p2=".$row['property2'] . "'><img src='/camera/camerapicdisplay.php?p1=". $row['property1'] . "&p2=".$row['property2'] . "'</img></a>";

where in i used to pass the needed data to next page .since GET is not best method to use . i wanted to pass it invisible to camera_rating.php .

A(Answer):

I don’t see what should be wrong with GET, otherwise you have to make it a form to use POST.

it will be especially a problem with the image url, because that’s not meant to be POSTed.

A(Answer):

okie !! if i continue with GET ! what are the ways so that i can mask the variables and values from users!!

A(Answer):

2. The most common way is using .htaccess mod_rewrite, but you will need to think of how your real URL relates to what you want it to look like.

yeah as i said earlier instead of url like

http://xyz.com/camera_rating.php?p1=nokia&p2=3310

it should look like

http://xyz.com/nokia3310

A(Answer):

this looks like a job for mod_rewrite (if you are on Apache)

A(Answer):

In your models database, allow for a field ‘model_alias’, or something like that. This field should correspond to what you want to display in the URL. So for your Brand: Nokia, Model: 3310 row, insert the alias: nokia3310. This is done because, without some extra processing, you wouldn’t know what nokia3310 points to in the database.

Then use a simple rewrite such as:


RewriteEngine On
RewriteRule ^/phone/(.*)$ /phone?alias=$1

You then have access to the alias via $_GET[‘alias’]. You can then use this data to pull the relevant information from the database.

A(Answer):

okie i got the concept very well ! i will store the nokia3310 in ma table .

since the proj i am working has come a long way is it possible to ,maintain the url like

http://xyz.com/mobile_rating.php?p1=nokia&p2=3310

and rewrite it to http://xyz.com/nokia3310

and i am on apache itself . i donot know much about rewrite

can you please tell what this piece of code is doing ?

RewriteRule ^/phone/(.*)$ /phone?alias=$1

A(Answer):

converting /phone/xyz to /phone?alias=xyz

A(Answer):

@pradeepjain

The difficulty with this is on what criteria do you process ‘nokia3310’. Of course you could say split the letters from the numbers and then use that, which would be the equivalent of ^([a-zA-Z]+)([0-9]+) – or something like that. This however does not bode well for maintenance; what if you have a phone that has numbers in it’s name, or a brand that has letters in it’s model?

You could then say have a specific rule for each model & brand, the equivalent of RewriteRule ^/phone/nokia3310$ /mobile_rating.php?p1=nokia&p2=3310 – of course there would be many more for each model & brand. But then when a new model or brand is created you would have to add this to the .htaccess file. The same goes for when you edit or delete a model or brand. I think you can see how this method is simply not viable.

There is another way using htaccess that you can perform a lookup using a RewriteMap, although I am not sure how quick (read: slow) this method is.

After considering the above options, I think you can see why I suggested using an alias as it drastically cuts down the work needed in maintenance and extensibility, etc. That said, however, you have to decide whether using an alias would require a rewrite of the existing code and whether that rewrite would be more expensive than one of the aforementioned methods.

– Mark.

P.S. There may be other methods that I have overlooked.

A(Answer):

@pradeepjain

See the documentation on mod_rewrite, more specifically the RewriteRule directive.

A(Answer):

okie i make the url look like

http://xyz.com/mobile_rating.php?nokia3310

can it be rewritten to without mobile_rating.php and all ?


and rewrite it to http://xyz.com/nokia3310

A(Answer):

Sure:


RewriteRule ^/(.*)$ /mobile_rating.php?$1

However, I recommend using an identifier because the above rule will match any requests under your root. Consider using the following structure: htxp://xyz.com/rating/nokia3310

– Mark.

A(Answer):

okie since i have diff pages 4 diff products like
mobile_rating.php
camera_rating.php

i can make it like
http://xyz.com/mobile/nokia3310
http://xyz.com/camera/sony120G

hope this is a gud way?

A(Answer):

That looks good, yes.

– Mark.

A(Answer):

i have a small doubt regarding the google search for the page urls . since ma pages are generated
when i search

nokia 3310 is google search and i have a page like

http://xyz.com/camera_rating.php?p1=nokia&p2=3310 will google recognize the page .

A(Answer):

ctd…. and how to make the website search recognise this page and display in search as its generated dynamically

A(Answer):

srry .. i am posting again and again …i am using a framework (drupal) i wrote the url rewrite and tried it out and b4 it came to the page its getting redirected to page not found ..

its coming to page http://www.xyz.com/mobile1/Nokia3110c ..since this page does not exists its going to page not found … is the

RewriteRule ^/mobile1/(.*)$ /mobile1?alias=$1

is the code i wrote in httpd.conf.

the default drupal installation requires the following rewriting

<Directory /var/www/html/ratingd>
Options FollowSymLinks
# AllowOverride None #orig
AllowOverride All
# additions for drupal rewriting
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] # RewriteRule ^/mobile1/(.*)$ /mobile1?alias=$1
</Directory>

wht must be done for the page to work?

A(Answer):

You are redirecting to a file named "mobile1." Are you sure you didn’t mean something along the lines of "mobile1.php"?

Also, that rule is after your index.php rule, meaning that the index.php rule will always be checked and executed first (since it will always meet that regex).

A(Answer):

since i am using a framework ".php" extension is not a mandatory . i just specify that its a php code that its, it takes care of rest. and the url does not correspond to file system files as url is stored in DB.

so what do u sugegst is the solution for this prob. the 1st index.php url rewrite is mandatory for the drupal to work and 2nd one is necessary for ma code .

A(Answer):

I don’t think you are getting the way that mod_rewrite works…

The index.php rewrite still has to be AFTER the mobile1 rewrite, otherwise it won’t be captured by the index.php rewrite. mobile1 is not a file on its own, index.php is. All requests have to be sent to a file, otherwise they will result in a server error.

A(Answer):

i tried to put the mobile1 rewrite rule b4 the index.php rule but still it did not work.
you mean to say tht for rewrite rule it must be an file in filesystem.

A(Answer):

… Did you uncomment it yet? Did you do any debugging at all? Show us your .htaccess file and check your logs.

A(Answer):

<Directory /var/www/html/ratingd>
Options FollowSymLinks
# AllowOverride None #orig
AllowOverride All
# additions for drupal rewriting
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/mobile1/(.*)$ /mobile1?alias=$1
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] </Directory>

i have written this in httpd.conf file and there is no .htaccess file available . wht debugging should be done ? i have never worked on rewriting . and i checked error_log .no error occurred when i loaded the page which lead to page not found

A(Answer):

<Directory "/var/www/html/rating/">
Options FollowSymLinks
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/rating1/camera/(.*)$ /rating1/camera/camera_rating.php?alias=$1
</Directory>

i took the codes out of the framework and wrote it in files and written the rewrite rule in httpd.conf then also its not working ..

it says the following error!!

[Fri Dec 18 11:38:33 2009] [error] [client 192.168.2.112] File does not exist: /var/www/html/rating1/camera/NikonD90, referer: http://192.168.2.213/rating1/camera/camera_display.php

A(Answer):

Why won’t you just use a .htaccess file? Your problem looks to be your rewrite base. Your rules simply aren’t being processed. Does the directory "/var/www/html/rating/" even exist? Are you sure that using "/" as your RewriteBase is valid in this situation?

A(Answer):

i wrote a .htaccess file for it with help of some example

RewriteEngine on
RewriteBase /rating1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^camera/(.*)$ /rating1/camera/camera_rating.php?alias=$1

this worked for me vey well with stand alone codings.

but when it comes to drupal framework its not working. bcos all pages are loaded from the index.php.which is like this

<?php
// $Id: index.php,v 1.94 2007/12/26 08:46:48 dries Exp $
/**
* @file
* The PHP page that serves all page requests on a Drupal installation.
*
* The routines here dispatch control to the appropriate handler, which then
* prints the appropriate page.
*
* All Drupal code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*/
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$return = menu_execute_active_handler();
// Menu status constants are integers; page content is a string.
if (is_int($return)) {
switch ($return) {
case MENU_NOT_FOUND:
drupal_not_found();
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
break;
case MENU_SITE_OFFLINE:
drupal_site_offline();
break;
}
}
elseif (isset($return)) {
// Print any value (including an empty string) except NULL or undefined:
print theme('page', $return);
}
drupal_page_foote();

dono how to get over this prob…some tips /help on this..

A(Answer):

That code says nothing. However, if you want to integrate your code into Drupal, you probably want it to go through index.php and not your custom PHP file. You are sending it to your own file.

And doesn’t Drupal have clean URL redirection as an option?

A(Answer):

yeah it already has clean url’s and its enabled… but since i want to direct it through custom pages its not working…:(. so i need to redirect it through index.php only is it… can u just tell me how to do it..some tips….

A(Answer):

Well, the way you are currently doing it does not integrate your actions into Drupal. There are ways to build modules into Drupal, but I couldn’t tell you how. I haven’t touched a CMS for years. I think you should investigate. The Drupal documentation for developing modules may be a good place to start.