Jump to content

.htaccess with PHP


gc40

Recommended Posts

Currently, I am using .htaccess rewrite to change some of the names of my URLs. However, I want to take it to another level.

 

Currently, here is what I have:

 

RewriteEngine On

 

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

 

So if my news article was located at: http://www.mydomain.com/newsarticles.php?id=3, it will now be http://www.mydomain.com/newsarticles/page/3

 

However, I want it a little different.

Rather than ending in with the ID number, I want the URL to have the title.

 

If the news article title is Cool News Article, then I want the URL to be http://www.mydomain.com/newsarticles/Cool-News-Article

 

Does anyone know what I would have to do to accomplish this task?

 

p.s. All news articles are pulled from a database and the title is stored in the field called `title`.

 

 

Link to comment
Share on other sites

In your newsarticles.php file, instead of searching your DB for an ID number (from $_GET['id']), have it search for the title: Example:

 

instead of

<?php
$id = $_GET['id'];
mysql_query("SELECT * FROM articles WHERE id='$id'");
?>

 

Do something like:

<?php
$title = substr("-"," ",$_GET['id']); // this changes Cool-News-Article into Cool News Article
mysql_query("SELECT * FROM articles WHERE title='$title'");
?>

 

Get what Im saying?

Link to comment
Share on other sites

I am lost still.

 

Here is my code for the actual page that will display the article:

 

<?php 
$current = 'training';
$fullpage = 'yes'; 

include("./class/config.php");

$id=$_REQUEST['id'];
$start=$_REQUEST['start'];
$sqlcontent="Select * from training where Training_ID='$id' LIMIT 1";
//$resprop=mysql_query($sqlprop,$db);
$rescontent=mysql_query($sqlcontent);
$onecontent=mysql_fetch_object($rescontent);
$row = mysql_fetch_array($rescontent);
?>
<?php include('header_top.php');?>
        <title><?php print ($onecontent->Training_Title); ?></title>
        <meta name="title" content="<?php print ($onecontent->Training_Title); ?>" />
        <meta name="keywords" content="<?php print ($onecontent->Training_Meta_Keywords); ?>" />
        <meta name="description" content="<?php print ($onecontent->Training_Meta_Description); ?>" />
        
<?php
include('header.php');	

        echo "<h1>".$onecontent->Training_Title."</h1>";
        echo "<p>".$onecontent->Training_Body."</p>";
	echo "<p><a href='javascript:history.go(-1)'>» Back</a></p>";

include('footer.php');
?>

Link to comment
Share on other sites

Change to something like:

 

<?php 
$current = 'training';
$fullpage = 'yes'; 

include("./class/config.php");

// i never use $_REQUEST by the way

$title = substr("-"," ",$_GET['id']); // get the title and replace - with 

$start=$_REQUEST['start'];
$sqlcontent="Select * from training where Training_Title='$title' LIMIT 1"; // you will need to edit your column name if i dont have it right

//$resprop=mysql_query($sqlprop,$db);
$rescontent=mysql_query($sqlcontent);
$onecontent=mysql_fetch_object($rescontent);
$row = mysql_fetch_array($rescontent);
?>
<?php include('header_top.php');?>
        <title><?php print ($onecontent->Training_Title); ?></title>
        <meta name="title" content="<?php print ($onecontent->Training_Title); ?>" />
        <meta name="keywords" content="<?php print ($onecontent->Training_Meta_Keywords); ?>" />
        <meta name="description" content="<?php print ($onecontent->Training_Meta_Description); ?>" />
        
<?php
include('header.php');	

        echo "<h1>".$onecontent->Training_Title."</h1>";
        echo "<p>".$onecontent->Training_Body."</p>";
	echo "<p><a href='javascript:history.go(-1)'>» Back</a></p>";

include('footer.php');
?>

 

Now just edit your .htaccess and you should be good to go

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.