Jump to content

SEO Friendly URL's


RyanMinor

Recommended Posts

I know this is a PHP forum, but I am completely lost as to why I cannot get mod_rewrite to give me SEO friendly URL's with my PHP website. I called GoDaddy and confirmed that mod_rewrite is enabled and turned on. I then copied a simple RewriteRule to send all requests to drupal.org and that worked. Now, I try to get my RewriteRules to work but everything I try fails. My .htaccess is placed in the same directory as the files it is dealing with. Below is what I have in my .htaccess file:

 


<IfModule mod_rewrite.c> 

RewriteEngine On
RewriteRule ^modelinfo/model/(.*)/?$ modelinfo.php?model=$1 [L]

</IfModule>

 

Let's say the URL that I am trying to clean up is: http://www.website.com/modelinfo.php?model=Model_Name (this is exactly what the URL looks like now) and I want it to say http://www.website.com/modelinfo/model/Model_Name. Can anybody help me out? Thanks in advance!

Link to comment
Share on other sites

mod_rewrite will not rewrite your existing urls within your files for your you. You'll need to do this setup yourself. For example change all links that look like this within your code

<a href="http://www.website.com/modelinfo.php?model=$Model_Name">$Model_Name</a>

To your new url format

<a href="http://www.website.com/mmodelinfo/model/$Model_Name">$Model_Name</a>

 

Going to website.com/modelinfo/model/Model_Name should call website.com/modelinfo.php?model=Model_Name

Link to comment
Share on other sites

mod_rewrite will not rewrite your existing urls within your files for your you. You'll need to do this setup yourself. For example change all links that look like this within your code

<a href="http://www.website.com/modelinfo.php?model=$Model_Name">$Model_Name</a>

To your new url format

<a href="http://www.website.com/mmodelinfo/model/$Model_Name">$Model_Name</a>

 

Going to website.com/modelinfo/model/Model_Name should call website.com/modelinfo.php?model=Model_Name

 

So, in my web page I need to recode all of my links? I thought the whole point of mod_rewrite was to take my dynamically created URL's and make them appear static. If I code what you want in my page, it will look for a modelinfo folder (which there isn't one). The actual dynamic URL in my website is this:

 

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "modelinfo.php?model=".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

Link to comment
Share on other sites

I thought the whole point of mod_rewrite was to take my dynamically created URL's and make them appear static.

No, this what a lot of people think mod_rewrite does. It cannot change your existing links within your code and transform them into your new url for you.

However you can add extra rewrite rules/conditions that will redirect your old urls to your new url.

 

Change your code to this to output your new url.

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "/modelinfo/".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

 

In modelinfo.php. You can still retrieve the model name from $_GET['model']

Link to comment
Share on other sites

Use this code to generate your links

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "/modelinfo/".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

 

And then just have this code within your .htaccess

<IfModule mod_rewrite.c> 

RewriteEngine On
RewriteRule ^modelinfo/model/(.*)/?$ modelinfo.php?model=$1 [L]

</IfModule>

That is all you need to do. Your code now outputs SEO friendly urls. And the .htaccess code maps a url like modelinfo/model/Model_Name/ to modelinfo.php?model=Model_Name

Link to comment
Share on other sites

I tried exactly what you showed me and I got a 404 Error. Could it be something with the page I am going to once clicking on a link (modelinfo.php)? Here is the code for that page.

 

<?php
// connect to database
require_once('includes/mysql.php');

// get model id from url
if (isset($_GET['model'])) {
$model = str_replace('_', ' ', $_GET['model']);
}

// get model information
$queryModelInfo = mysql_query("SELECT modelname, modelphoto, modelvideo, modelbio FROM tblmodel WHERE modelname = '$model'", $mysql) or die(mysql_error());
$dataModelInfo = mysql_fetch_assoc($queryModelInfo);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
<script src="flowplayer/flowplayer-3.2.2.min.js"></script>
</head>
<body>

<?php include ('logo.php'); ?>

<hr />

<div id="header"><?php include ('menu.php'); ?></div>

<div id="page">

<div id="content">

<div class="post">
<h2 class="style3"><?php echo stripslashes($dataModelInfo['modelname']); ?></h2>

<div class="entry">

<table width="95%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
	<td align="center">
		<a href="model_preview_videos/<?php echo $dataModelInfo['modelvideo']; ?>" style="display:block;width:425px;height:300px;" id="player"></a>
		<script language="JavaScript">
			flowplayer("player", "flowplayer/flowplayer-3.2.2.swf", {
				version: [9, 115]
			});
		</script>
	</td>
</tr>
<tr>
	<td>
	<?php echo stripslashes($dataModelInfo['modelbio']); ?>
	</td>
</tr>
</table>

</div>

</div>

</div>

<?php include ('storepromo.php'); ?>

<div style="clear: both;"> </div>

</div>

<?php include ('footer.php'); ?>

</body>
</html>

<?php mysql_free_result($queryModelInfo); ?>
<?php mysql_close($mysql); ?>

 

Use this code to generate your links

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "/modelinfo/".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

 

And then just have this code within your .htaccess

<IfModule mod_rewrite.c> 

RewriteEngine On
RewriteRule ^modelinfo/model/(.*)/?$ modelinfo.php?model=$1 [L]

</IfModule>

That is all you need to do. Your code now outputs SEO friendly urls. And the .htaccess code maps a url like modelinfo/model/Model_Name/ to modelinfo.php?model=Model_Name

Link to comment
Share on other sites

There is a problem with the code I gave for the links,

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "/modelinfo/".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

It should be

$model = str_replace(' ', '_', $dataModels['modelname']);
$Link = "/modelinfo/model/".$model;
echo "<a href='$Link'><img src = '$Thumb' border='0'></a>";

I didn't add the /model/ part to the link.

Link to comment
Share on other sites

Are you sure mod_rewrite is enabled on your server. To test this remove <IfModule mod_rewrite.c> and </IfModule> from your .htaccess file. If mod rewrite is not enabled the server should display a 500 Internal Server Error message. To fix this you'll need to contact your host for support.

Link to comment
Share on other sites

Are you sure mod_rewrite is enabled on your server. To test this remove <IfModule mod_rewrite.c> and </IfModule> from your .htaccess file. If mod rewrite is not enabled the server should display a 500 Internal Server Error message. To fix this you'll need to contact your host for support.

 

This was one of my original thoughts, so I went ahead and called GoDaddy and they ensured me that mod_rewrite is enable and active on my server. Removing the <IfModule mod_rewrite.c> </IfModule> does nothing different that having it in my .htaccess.

Link to comment
Share on other sites

Does the orignal url still work okay? (website.com/modelinfo.php?model=Model_Name)? But the url website.com/modelinfo/model/Model_Name/ doesn't?

I have tested the rewrite rule and the code that creates the links. They both work fine for me.

Link to comment
Share on other sites

Yes, the original link (www.website.com/modelinfo.php?model=Model_Name) still works fine while the new link (www.website.com/modelinfo/model/Model_Name) does not work.

 

Does the orignal url still work okay? (website.com/modelinfo.php?model=Model_Name)? But the url website.com/modelinfo/model/Model_Name/ doesn't?

I have tested the rewrite rule and the code that creates the links. They both work fine for me.

Link to comment
Share on other sites

I was finally able to get my SEO friendly URLS's working, but when I am taken to the modelinfo/model/Model_Name page, none of my CSS formatting or images show. I tried adjusting the include paths to the entire path but even that didn't work. Does anybody know what might be going on there. I had to parse the URL to get the correct value to use in the query for the information that is to be displayed on modelinfo, but eventually got that to work. Now I just need to get the page to show correctly.

Link to comment
Share on other sites

Okay it worked now. The only problem is my links to the images in my included files all off. I know how to fix that though. I think I will slowly convert the website to SEO friendly URL's since it's going to be quite a hassle. Thanks for all of your help!

 

When linking to your css, javascript, image files etc. start the url path with a /. Otherwise the web browser will trying to load your files from http://website.com/modelinfo/model/ rather than http://website.com/. Example

<link rel="stylesheet" href="/your/css/file.css" />

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.