How to change my id and name in php url

7,389

As I understand, you want requests for the following URL website.com/games/271/game-name-here to be handled by games.php

For this, you may use a simpler rule.

RewriteRule ^games/(.*)$ /games.php?var=$1 [L]

This will send all requests with /games/ to games.php. For e.g.

/games/271/game-name-here will go like this /games.php?var=271/game-name-here

Now, game-name-here is only for SEO purpose as what you actually need is only the id. So, filter the id part from the var like this -

$value=($_GET['var']);
$temp = explode('/',$value);
$id = $temp[0];

Remember, htaccess is used only for handling the URL requests. You still need to form the correct URLs in your webpages.

Additional Tip: You may also consider the following URL structure website.com/games/271-game-name-here . Here just explode the URL on a hyphen '-' instead of '/'

Share:
7,389

Related videos on Youtube

slixxed
Author by

slixxed

Updated on September 18, 2022

Comments

  • slixxed
    slixxed over 1 year

    How would I change the URL from

    from: example.com/games.php?id=27

    to: website.com/games/271/game-name-here

    So far I have this in .htaccess:

    RewriteEngine on
    RewriteRule ^games/([0-9]+)/?$ games.php?id=$1 [NC,L]
    RewriteRule ^(.*).aspx$ $1.php
    RewriteRule ^(.*).ashx$ $1.php
    Options -Indexes
    <Files 403.shtml>
    order allow,deny
    allow from all
    </Files>
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    Options -Multiviews
    
  • MrWhite
    MrWhite over 8 years
    This answer is inconsistent. In the first code snippet PATH_INFO is used (ie. abc.php/path/info) to create a more friendly looking URL (which is different to what the OP requires). If PATH_INFO is used then you don't necessarily need to do any rewriting in .htaccess, but your second code snippet implies that you are.