简体   繁体   中英

SEO friendly URL using smarty php?

I am using smarty PHP in my project.

normally I use the following code in smarty to create links and point them to their specific URL's:

in my smarty template page:

{section name=title loop=$title}
<li><a class="nav" href="index.php?url={$title[title].url}">{$title[title].title}</a></li>
{/section}

the code above will generate the following URL:

http://domain.com/index.php?url=somename.html

the somename.html is stored in the mysql database.

in my index.php file I get the details of the url=someone.html like so:

in my smarty template page:

{if isset($smarty.get.url)} 


  {$body}

  {/if}

and in my php page:

if(isset($_GET['url']))
{
    include "config/connect.php";

    $url = preg_replace('#[^0-9]#i', '', $_GET['url']);
    $url=mysqli_real_escape_string($db_conx, $_GET['url']);
if ($stmt = mysqli_prepare($db_conx, "SELECT id, url, title, body FROM pages WHERE url=?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $url);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $id, $url, $title, $body);
    /* fetch value */
    mysqli_stmt_fetch($stmt);





$pageurl=$_GET['url'];
    /* close statement */
    mysqli_stmt_close($stmt);
}
}
/* close connection */
mysqli_close($db_conx);


$smarty->assign('id', $id);
$smarty->assign('url', $url);

$smarty->assign('title', $title);
$smarty->assign ('body', $body);   

This all works fine up to this point.

the question I have is this:

I am trying to create SEO friendly URL's.

example:

http://domain.com/index.php?url=somename.html

to

http://domain.com/somename.html

I have this code in my htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ index.php?url=$1

the code above will allow me to convert http://domain.com/index.php?url=somename.html

to http://domain.com/somename.html

BUT,

when I view http://domain.com/somename.html in the browser, I get a blank page, which means I do not get the related information for somename.html .

for example if i run http://domain.com/index.php?url=somename.html in the browser, I get the {$body} of the somename.html or something.html etc in my page.

but if i run http://domain.com/somename.html in the browser, I get nothing as it seems I do not have access to the $url = preg_replace('#[^0-9]#i', '', $_GET['url']);

is there anything that I need to do with smarty PHP or htaccess file in order to make this work?

ANY HELP WOULD BE APPRECIATED.

The problem is in your rewrite rule

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

the .html is not included inside the () thus $1 contains everything before the .html

eg http://domain.com/somename.html gets rewritten to http://domain.com/index.php?url=somename

The better rewrite rule, per Pinoniq's answer, would be:

 RewriteRule ^([\w/]+\.html)$ index.php?url=$1

That should catch any alphanumerics plus the forward slash, and the .html but with the . escaped to avoid that headache.

Alternatively, you could also go with:

 RewriteRule ^([\w/]+).html$ index.php?url=$1.html

and just add the .html to the rewritten URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM