How to change my id and name in php url
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, mod-rewrite, url-rewriting, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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
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 '/'
Basically you need to do two things :
.htaccessto decode the url and send it to abc.php- You need to change the file that create the URL. If the page that refer to
abc.php?vid=55isindex.php, you need to change your code inindex.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.
Comments
Post a Comment