Reputation: 4141
I would like to make URLs on my website a bit nicer.
Currently, single product pages are in form of:
www.mywebsite.com/details.php?id=1
and I would like those to be more like
www.mywebsite.com/name-of-the-product?id=1
where id=1 is changeable and it will be id of specific product.
So I would like to redirect all new links to old page named details.php
, passing id to it.
I searched through some answers, but all of those are for predefined and specific GET params, I would like this to be more general, for all products on website.
updated
Thanks everyone for help! It looks like I misleaded you. I have numerous pages on website. And I want to change routes only for this one (single product pages, so that those particular routes, look nicer). Right now, only @samurai 's answer does redirecting, but it redirect's also every other page. That's why I was thinking to add some specific GET param (or something like that), to tell .htaccess 'hey, only redirect pages like this to details.php'. Is it possible to do it like that?
Upvotes: 0
Views: 395
Reputation: 2204
Create an .htaccess file and put those lines in it:
RewriteEngine on
# URL rewriting rules
RewriteRule ^([_a-zA-Z0-9-]+)/([0-9]+)$ details.php?id=$2 [L,E]
and your url should be : www.mywebsite.com/name-of-the-product/1
or
RewriteRule ^([_a-zA-Z0-9-]+)?$ details.php [L,E]
to have a url like this : www.mywebsite.com/name-of-the-product?id=1
Upvotes: 0
Reputation: 794
Why to keep id in url when it will show product name? Anyway, what you can do is get product name from database based on supplied id, head page to desired url and add some .htaccess magic
UPD: Lets take Nike Air Max as an example. user tries to access next url: www.mywebsite.com/?id=1
, while in your database Air Maxes assosiated with id=1. Piece of php code of ur page:
$id = intval( $_GET['id'], 10 );
$product_name = mysqli_fetch_assoc(mysqli_query($con, "SELECT item_name FROM products WHERE id = $id"));
$altered_name = str_replace(' ', '-', $product_name);
header('Location: /?id=1&product_name='.$altered_name);
And now some .htaccess magic
RewriteRule ^product/([^/]*)$ /?id=1&product_name=$1 [L]
As a result, url will be www.website.com/product/nike-air-maxes
Upvotes: 1
Reputation: 20737
The internal rewrite would look like this:
RewriteRule ^(?!details\.php)[^/]+/?$ details.php [L]
The query string is automatically carried over. The (?details\.php)
part is a negative look-ahead. It prevents details.php from matching this rule, as this would cause an infinite loop. Please note that you should:
Check in details.php if an id is supplied. If it isn't supplied, you should do your database-magic to obtain this id, then redirect the client in php
if( empty( $_GET['id'] ) ) {
$id = getIdByName( $name );
header( "Location: {$name}?id={$id}", 301 );
exit();
}
Check if details.php is not requested directly. You cannot summon the product name from thin air in .htaccess, so you need to do this in your php script
if( empty( $name ) || $name == "details.php" ) {
$name = getNameById( $id );
header( "Location: {$name}?id={$id}", 301 );
exit();
}
Upvotes: 1