Jump to content

Add post title in the end of url


AbhinavDubey

Recommended Posts

I have a website called gapagap.com developed using php.

 

What is basically want is to change the url pattern from:

 

http://www.gapagap.com/gag/54

 

To this:

 

gapagap.com/gag/then-why-am-i-not-a-millionaire-yet

 

So what I basically want is to add the post title in the end of the url instead of the post sequence number. 

Link to comment
https://forums.phpfreaks.com/topic/276639-add-post-title-in-the-end-of-url/
Share on other sites

include("include/config.php");

include("include/functions/import.php");

$thebaseurl = $config['baseurl'];

 

function getPageUrl(){

     static $pageURL = '';

     if(empty($pageURL)){

          $pageURL = 'http';

          if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')$pageURL .= 's';

          $pageURL .= '://';

          if($_SERVER['SERVER_PORT'] != '80')$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];

          else $pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

     }

     return $pageURL;


 

if (is_numeric($_REQUEST['pid']))

{

$pid = $_REQUEST['pid'];

STemplate::assign('pid',$pid);

}

else

{

$error = $lang['138'];

}

 

 

Here it is. There isn't any framework used. The code is developed using a normal text editor.

I developed this code

 

To add the php file 

 

 

function create_slug($string){
   $string = preg_replace( '/[«»""!?,.!@£$%^&*{};:()]+/', '', $string );
   $string = strtolower($string);
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
 
$slug = create_slug('This is my page title');
 
echo $slug;
 
 
To htaccess file
 
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1 [QSA]
 
 
Will this help. I haven't tested it yet

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.