Jump to content

PHP add some string to filename in address bar and display


Recommended Posts

Hello ,
I have two fields in database (ID , CITYNAME ) like below

123 |  DELHI




125 |  PUNJAB



I am trying to display records based on id number in a file called viewrecords.php.
where i am passing ID parameter (http://www.example.com/viewrecords.php?ID=123) and display the record in viewrecords.php file.
It simply displays the records where the corresponding id is matches.
Now I have a requirement where i need to display a city field ( for example http://www.example.com/cityname-ID.php that is http://www.example.com/delhi-123.php) in the addressbar instead of viewrecords.php.


any help appreciated.

Link to comment
Share on other sites

Hi Thanks for the reply.


in my end both city name and is dynamic where city user enters. so I need to create the page in run time. 


So I see two possibilities here..


one is to create the filename entirely in runtime.


second one is to have the filename already like $city-$id.php


But i am not sure these are possible. 


Please let me know ...

Link to comment
Share on other sites

Oh now I understand. 

What you'll want to look at is mod_rewrite. You don't dynamically create new files on the fly. 


So if you want your urls to be site.com/delphi-123 (like this forum) you'd use the following mod_rewrite rule a in a .htaccess file

RewriteEngine On

RewriteRule ([a-z+])-([0-9+)/ viewrecords.php?ID=$2&cityname=$1 [NC]

The above rewrite rule will match a url like site.com/delphi-123 and then pass delphi and 123 as querystring parameters to viewrecords.php.



Now for your code create that url you'd use this

$id   = $row['ID'];
$city = $row['CITYNAME'];

echo "<a href=\"/$city-$id/\">$city</a>";
Link to comment
Share on other sites



I understand the concept you explained. But however If i use the above code, my entire site shows as 500 Internal Server Error.


Hi I tried the following code :  just alone with ID.


RewriteEngine On

RewriteRule (.*)/$ load_page.php?&page_id=$1



it is working with slash in end of the url.  ( http://www.example.com/384/) 

and not working without the slash in the end of the url. (http://www.example.com/384)


any suggestions 

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.