Jump to content

MOD Rewrite


adamjblakey

Recommended Posts

Hi,

 

What i am trying to achieve is convert all my urls to output a valid seo url.

 

For example:

 

If you go here: http://www.foundrecipes.com/recipes.php?type=all and select a category you would get something like http://www.foundrecipes.com/recipes.php?category=Automatic%20Bread%20Machine which is not a seo friendly url. What i would like is when a category is click the url would appear like so e.g. http://www.foundrecipes.com/recipes/category/Automatic-Bread-Machine.html i don't know if this can be done simply with a htaccess file or would i need to have something in the recipes.php page?

 

Also this applies to when i click on a recipe, so for example if i click on the relevant recipe i wanted to see it would take me to e.g. http://www.foundrecipes.com/recipe-details.php?title=apple%20roquefort%20bread but i want this to output like e.g. http://www.foundrecipes.com/recipedetails/apple-roquefort-bread.html

 

Thanks in advance for your help.

Adam

Link to comment
Share on other sites

Hmm.. You will need to do it in both .htaccess and your recipes.php, as for the .htaccess, this is what I did for my <a href="http://howtoinstallscript.com/staff.html">Staff List</a>, you can see it when you clicked on the Staff name.

 

.htaccess

RewriteEngine on #start of mod rewrite
RewriteBase // #if you have a folder, then put the folder name there
RewriteRule ^recipes/category/(.*).html/ recipes.php?category=$1 #mod rewrite

 

On your recipes.php you should contain this to help you change your dynamic url to static url,

recipes.php (to display the static URLs)

<?php
# $row['category_title'] is your Category title
$cat_url = strtr($row['category_title'], "éèêàëâúóíáABCDEFGHIJKLMNOPQRSTUVWXYZ. ","eeeaeauoiaabcdefghijklmnopqrstuvwxyz--");
$cat_url = ereg_replace('[^a-zA-Z0-9_-]', '', $cat_url);
# then to echo out it should be
# echo '<a href="http://www.foundrecipes.com/recipes/category/'.$cat_url.'.html">'.$row['category_title'].'</a>';
?>

 

recipes.php (mysql query to find the category)

<?php
$query = "SELECT * FROM category"; #your query
$result = mysql_query($query) or die("MySQL Error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$cat_url = strtr($row['category_title'], "éèêàëâúóíáABCDEFGHIJKLMNOPQRSTUVWXYZ. ","eeeaeauoiaabcdefghijklmnopqrstuvwxyz--");
$cat_url = ereg_replace('[^a-zA-Z0-9_-]', '', $cat_url);
if ($cat_url == ereg_replace('[^a-z0-9]', '', $_GET['category'])) {
    	#echo out the result
        $search_result = 'found';
    }
}
if ($search_result != 'found') {
    	#echo 'Category not found!';
    }
?>

 

Hope this helps, all the best!

Link to comment
Share on other sites

Thank you for your reply.

 

This is what i have done so far but does not seem to work

 

$selects = "SELECT * FROM recipes WHERE category = '$_GET[category]'";
	$result = mysql_query($selects);				
	$countrowssa = mysql_num_rows($result);

		if ($countrowssa < 1) {
			print "<b>There are currently no recipes with this criteria.</b>";
		}else {

			while ($res = mysql_fetch_array($result)) {
			$resu[] = $res;	
		}

			mysql_free_result($result);
			foreach ($resu AS $row) {
			$cat_url = strtr($row['title'], "éèêàëâúóíáABCDEFGHIJKLMNOPQRSTUVWXYZ. ","eeeaeauoiaabcdefghijklmnopqrstuvwxyz--");
$cat_url = ereg_replace('[^a-zA-Z0-9_-]', '', $cat_url);
			echo "<li><a href='recipes/$cat_url.html'>".$row['title']."</a></li>";
		}
		}

 

Then i have added this to my htaccess file

 

RewriteEngine On
RewriteRule ^recipes/(.*)\.html$ recipe.php?category=$1

 

If i go to e.g. http://www.foundrecipes.com/recipes.php?category=Automatic%20Bread%20Machine it is changes the links to the next page but these links do not work.

 

Is this setup correctly?

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.