Jump to content

Clean URL's for multiple pages


happypete
Go to solution Solved by cataiin,

Recommended Posts

I am working on a database drive website. On my index page I have a menu like this:

<ul>
    <li> <a href="item1.php">Item 1</a></li>
    <li> <a href="item2.php">Item 2</a></li>
    <li> <a href="item3.php">Item 3</a></li>
    <li> <a href="item4.php">Item 3</a></li>
</ul>

I currently have created separate pages (item1.php / item2.php / item3.php....) Each page calls data from a database based on row 'id': 'item1.php' uses: SELECT * FROM items WHERE id = 1 etc.

All 'item' pages are identical except for the fact that they call different rows from the database: item1.php calls id=1, item2.php calls id=2 etc

I'm thinking to use a single page (
item.php) and populate it based on the $_GET['id'] ie: item.php?id=1

Is there way to write clean URL's with .htaccess and have it so if clicking on the item1.php menu in the index page that it will call item.php but
display it as item1.php and fill it will details from the database based on id=1 ?

The idea is that I will have hundreds of pages item1.php to item999.php but don't want to have to create a new page for each database row when all pages will be identical except for the fact that they import a different row from the databse. At the same time I want pages item1.php to item999.php to be index by the search engines...

I hope I have explained this properly...?

Link to comment
Share on other sites

You can use $_GET['id]; in item.php page to get item.php?id=1 (content from current item1.php) and then with RewriteRule:

RewriteRule ^([0-9]+)$ /item.php?id=$1

site.com/1 = site.com/item1.php

site.com/n = site.com/itemn.php

Ask if you have more questions.
Link to comment
Share on other sites

 

Ask if you have more questions.

 

 

Yes, sorry I'm still confused, I just can't get my head round it...

 

index.php

 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
</head>
<body>
<ul>
    <li> <a href="item1.php">Item 1</a></li>
    <li> <a href="item2.php">Item 2</a></li>
    <li> <a href="item3.php">Item 3</a></li>
    <li> <a href="item4.php">Item 3</a></li>
</ul>
</body>
</html>

item.php

<?php $item = $_GET['id']; ?>
 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
</head>
<body>
<?php echo $item; ?>
</body>
</html>

.htaccess

RewriteEngine on
RewriteRule ^([0-9]+)$ /item.php?id=$1

So if I click item1.php in the index.php page I want to be directed to item1.php  and $item should be '1'

 

item1.php doesn't exist.....

Edited by happypete
Link to comment
Share on other sites

  • Solution

index.php

<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<ul>
<li><a href="/1">Item 1</a></li>
<li><a href="/2">Item 2</a></li>
<li><a href="/3">Item 3</a></li>
<li>etc</li>
</ul>
</body>
</html>

item.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
$id = $_GET['id'];
$connection = mysqli_connect("localhost", "user", "pass", "database");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " .mysqli_connect_error();
}
$result = mysqli_query($connection,"SELECT * FROM items WHERE id='".$id."'");
while($row = mysqli_fetch_array($result))
{
var_dump($row);
}
mysqli_close($connection);
?>
</body>
</html>
 
and .htaccess
RewriteEngine on
RewriteRule ^([0-9]+)$ /item.php?id=$1

Sorry but my english sucks. :D

Edited by cataiin
Link to comment
Share on other sites

awesome, thanks very much, it works :)

 

I added this: [a-z]+ to the .htaccess file:

RewriteEngine on
RewriteRule ^([a-z]+[0-9]+)$ /item.php?id=$1

then changed the links to this, so it would show link1 as opposed to just 1

<ul>
    <li> <a href="/link1">Item 1</a></li>
    <li> <a href="/link2">Item 2</a></li>
    <li> <a href="/link3">Item 3</a></li>
    <li> <a href="/link4">Item 3</a></li>
</ul>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.