1

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
1

2 Answers 2

1

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 '/'

0

Basically you need to do two things :

  1. .htaccess to decode the url and send it to abc.php
  2. You need to change the file that create the URL. If the page that refer to abc.php?vid=55 is index.php, you need to change your code in index.php

For example:

old program

$query = "SELECT id,title FROM vid_table";
$result = mysql_query($query);

while ($data = mysql_fetch_array($result))
{
    echo '<a href="abc.php?vid=' . $data['id'] . '">' . $data['title'] . '</a>';
}

new program

while ($data = mysql_fetch_array($result))
{
    echo '<a href="abc.php/vid/' . str_replace(" ", "-", $data['title']) . ">' . $data['title'] . '</a>';
}

In abc.php you need to change the way the program read from the database

/* this means that the page get redirected from .htaccess */
if (isset($_SERVER['REDIRECT_URL'])) 
{
    $parts = explode('/', $_SERVER['REDIRECT_URL']);

    array_shift($parts);

    $url_name = $parts[1];
    $url_name = str_replace("-", " ", $url_name);
    $query = "SELECT id FROM vid_table WHERE title = '$url_name'";
    $result = mysql_query($query);
    $data = mysql_fetch_array($result);
    $_GET['id'] = $data['id'];
}

Then you can continue with your program and can go to second step and URL rewriting.

More information:
http://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/

I hope it's not too confusing.

1
  • 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.
    – MrWhite
    Commented Sep 3, 2015 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.