How to create specific URL for every page of a website

Hello sitepoint community.

I don’t have as much experience with PHP as I have with c and c++, so I am not new to programming but pretty new to web programming.

My question is about, how to do specific Url for a content. That means, I have for exemple a view.php file, that reads from a txt file and echos it on the page. Now, I have 5 .txt files: file_1.txt, file_2.txt … file_5.txt. And I want to have 5 different urls specific to this files.

How do I do that? I want to have like: domain.com/writer.php/file_2.txt and make it so that my file reading function in writer.php gets file_2.txt specifically.

I am sure this has an easy way to do it. I mean I can try and code something complicated with SESSION and POST variables. But I think there should be an easiest, and commonly used way.

I really tried to research this on the internet. And I am sure there is the easy answer somewhere on the net. But I just couldn’t formulate it the right for when I googled for it.

Your help is appreciated. Thanks.

1 Like

If I understand your needs correctly you’ll probably want to use .htaccess for this rather than PHP.

There is a good tutorial by @dklynn here

2 Likes

Hi samilkarahisar welcome to the forum

Years ago most sites had a file for each page and the system file directory structure closely resembled the URLs eg.

domain.com/index.php was in the root folder.
domain.com/widgets/green.php was in the widgets folder inside of the root folder etc.

Over the years “pretty URLs” and routing have become more popular.

That is “.html” or “.php” are not part of the URL and a lot of times HTTP requests don’t go directly to an actual file of that name, but result in a page getting displayed comprised of content put together on the server.

If you have only a few pages and you don’t mind the “old fashioned” URLs. the simple folder / file structure is easy work. You can use PHP to build the resulting page that is output to the browser from various pieces whether it be various assets, CSV, XML, text files, other PHP files, a database or other sources.

Thinking ahead about the goal is a good thing, but if you don’t have a good grasp of HTML yet, I strongly recommend you start there.

2 Likes

Htaccess might be what I am looking for, but I couldn’t neither on your link nor on the internet find how. Could you be a little bit more specific?

The link worked for me. It is dk.co.nz/seo - hope that works!

2 Likes

A common way to do something like this is with URL variables and GET.
So the URL may look like:-

http://www.example.com/thisfolder/?page=2

The index.php file in that folder will contain something like:-

if(isset($_GET['page'])) {
    $pn = preg_replace('#[^0-9]#i', '', $_GET['page']) ; // Filter and get the page number (pn)

    $filename = 'file_' . $pn . '.txt' ; // Put the page number into the filename string
}

Which tells the script which file to read.

2 Likes

this is exactly what I needed, thank you.

just to finish with this, $GET[‘page’] variable is the number that should come after file right?

page is the url variable, which in the example is equal to 2: ?page=2
$_GET gets the value of whatever variable you ask for, so $pn gets the value of 2 in this case.
In its simplest form, $pn = $_GET['page']
The 'preg_replace` is there to filter out any non numeric data, for security and prevention of error.

I would be tempted to get_file_contents(“textFile_1.txt”) into either a PHP or HTML file:

<?php
declare(strict_types=1); // PHP 7 ONLY
error_reporting(-1);
ini_set('display_errors', 'true');


//================================
function fred($val)
{
  echo '<pre>';
    print_r($val);
  echo '</pre>';
}

$sp    = 'https://www.sitepoint.com/community/t/how-to-create-specific-url-for-every-page-of-a-website/216211';  
$title = ' How to create specific URL for every page of a website';
$files = [ 
'0' => 'file_1.txt',
'1' => 'file_2.txt',
'2' => 'file_3.txt',
'3' => 'file_4.txt',
'4' => 'file_5.txt',
];

$pages = [];
foreach($files as $item => $textFile)
{
  $pages[] = file_get_contents($textFile);
}


?>
<!doctype html>
<html lang="en">
<head>
<title> <?=$title;?> </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" >
<style>
.bdr {outline:solid 1px #aaa; border-radius:0.88em;} 
.bg0 {background-color: #000; color:aqua;}
.cla {color:aqua;}
.clb {clear:both;}
.clg {color: darkgreen;} .clr {color:red;}
.fll {float:left} .flr {float:right;}
.mga {margin:1em auto 2em;}
.hg5 {height:5em;}
.ooo {margin:0; padding:0;}
.p42 {padding:0.42em;}
.tac {text-align: center;}
.w42 {width:42%;} .w88 {width:88%;}
.menu a       {padding:0.12em 0.88em; margin:0.42em 1em; background-color: #eef; color:#333;}
.menu a:hover {background-color: #eee; color:#00f;}
</style>
</head>
<body class="ooo">
<div class="ooo hg5 bg0 tac">
  <b class="flr"> <a href="<?=$sp;?>"><b class="cla">SitePoint</b> </a> </b>
  <h1 class="ooo clb"><?=$title;?></h1>
</div>

<div class="menu w42 mga">
  <?php 
    foreach($pages as $page)
    {
      echo '<a href="' .$page .'">' .$page .'</a>';
    }
  ?>
</div>

<?php if( 1 || isset($_GET['src']) ) : ?>
  <p class="clb">&nbsp;</p>

  <div class="w88 mga bdr p42">
    <em>Source:</em>
    <hr>    
    <?php highlight_file(__FILE__);?>
  </div>
<?php endif ?>

</body>
</html>
1 Like

Oh sweet lord of codes, I just understood how a url works. Thank you Sam.

I actually did know how to use GET, and POST to send variables between pages.

But I didnt know that when I have --file.php?variable=1, that actually sent 1 in $_GET[‘variable’] to file.php.

Is that right?

1 Like

Yes, that’s it.
http://php.net/manual/en/reserved.variables.get.php

2 Likes

And more. You could tack on a mess of them (though I wouldn’t get carried away doing so) eg.

?pn=3&amp;pref=plain&amp;loc=usa&amp;brwsr=ie

etc. etc. take a look at some of the GET vars Google uses in the address bar when you search

3 Likes

You guys are awesome, I just got what I wanted work on my website.

Btw yes Mittineague, I will definitely try more complicated codes with what I understood. And I will also use htaccess to hide the url names, and show users a cleaner link, just like youtube does for exemple.

Thank you guys again and again. Great community.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.