0

I have the following PHP code which is a file name I'm using as the title of my pages for each download I provide to viewers. I want to be able to have SEO-friendly titles and URLs.

<?php echo $file_info[21]; ?>

The URLs of each page look similar to this:

http://site.com/directory/download_interim_finder.php?it=ZWtkaW1rcmInbn1ma3h9aXFLcHN6fWVzeDgnISJoO3tran0reW97cSd1YHR5YHZIbWBmfzxwY3tiOnxycnA8Y25zfWpna3Y+YXtlZnl7dnJLf3J8cWd2fT4kJCIramFbcm1tbWRqdHJmJnR2ZGhKd3JpdShQdGV+bX09fWh0b2JnfXx9anV2J3F7YHh4d3Z2S3Jyf3ZmdX8pNyg1anZ/bnhpdGJwVnx5cXF5Zn5bYW14cHp2bTQiKXFsYw==

I would like to convert this long URL to something a little more SEO-friendly and that will incorporate the php code I have specified above to something like "filename.php" instead of the long URL you see here.

I was hoping to use .htaccess but I've read that you can't do it through there. I've spent hours looking for ways to fix this and can't find a solution. Any help would be appreciated.

Thanks!

2 Answers 2

1

Step By Step instruction about create SEO friendly URL with dynamic content using PHP and .htaccess mod redirection. Friendly URLs improves your site search engines ranking. Before trying this you have to enable mod_rewrite.so module at httpd.conf. It’s simple just few lines of PHP code converting title data to clean URL format.

Database

Sample database blog table columns id, title, body and url.

CREATE TABLE `blog`
(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` TEXT UNIQUE,
`body` TEXT,
`url` TEXT UNIQUE,
);

Publish.php

Contains PHP code. Converting title text to friendly url formate and storing into blog table.

<?php
include('db.php');
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string);
  return implode(' ', array_slice($words, 0, $word_limit));
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
  $title = mysql_real_escape_string($_POST['title']);
  $body = mysql_real_escape_string($_POST['body']);
  $title = htmlentities($title);
  $body = htmlentities($body);
  $date = date("Y/m/d");

  //Title to friendly URL conversion
  $newtitle = string_limit_words($title, 6); // First 6 words
  $urltitle = preg_replace('/[^a-z0-9]/i', ' ', $newtitle);
  $newurltitle = str_replace(" ", "-", $newtitle);
  $url = $date . '/' . $newurltitle . '.html'; // Final URL

  //Inserting values into my_blog table
  mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}

?>
<!--HTML Part-->
<form method="post" action="">
  Title:
  <input type="text" name="title"/>
  Body:
  <textarea name="body"></textarea>
  <input type="submit" value=" Publish "/>
</form>

Article.php

Contains HTML and PHP code. Displaying content from blog table.

<?php
include('db.php');
if($_GET['url'])
{
  $url  =mysql_real_escape_string($_GET['url']);
  $url = $url . '.html'; //Friendly URL
  $sql = mysql_query("select title,body from blog where url='$url'");
  $count = mysql_num_rows($sql);
  $row = mysql_fetch_array($sql);
  $title = $row['title'];
  $body = $row['body'];
}
else
{
 echo '404 Page.';
}
?>
<!-- HTML Part -->
<body>
  <?php
  if($count)
  {
    echo "<h1>$title</h1><div class='body'>$body</div>";
  }
  else
  {
    echo "<h1>404 Page.</h1>";
  }
  ?>
</body>

.htaccess

URL rewriting file. Redirecting original URL 9lessons.info/article.php?url=test.html to 9lessons.info/test.html

RewriteEngine On

RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1
0

When I look at the URL example you posted I see two things:

  • It points to a PHP script: /directory/download_interim_finder.php
  • It has a (looks like a) Base64 encoded argument which obviously no human can read

I'm guessing your input for the download script is encoded in it-argument string, you will need to put this in the URL else you cant identify which file you want to download. Since you want your URLs to be semantic you have to add your semantic information to the URL.

You could for instance make URLs like:

/download/documentation/productX/[[Base64encodedinputstring]]

In your .htaccess you then need the rule:

RewriteEngine On
RewriteRule /download/.*/([^\/]+) /directory/download_interim_finder.php?it=$2

If your host does not allow you to use .htaccess then you can not easily imploy semantic urls.

0

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.