URL rewriting dynamic URL of my webshop.

Discuss practical ways rearrange URLs using mod_rewrite.

Postby richardk » Fri Oct 17, 2008 8:38 am

Code .htaccess:

You should use
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.nl$ [NC]
RewriteRule ^(.*)$ http://www.example.nl/$1 [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /product.php?categorie=$1&subcategorie=$2&slug=$3 [QSA,L]


1. Images wont show up aymore. What should I add or change in the .htaccess for this?

Read the FAQ i previously linked to. You do not add anything to your .htaccess file, you add a <base> to your HTML.

2. The URL still contains %20 (spaces) which I want to convert to dashes. When I manually link with dashes it does work, but there is no way to do this automatcally now because the variables in my database d not contain hyphens.

Use PHP to do a str_replace() when 0outputting the links and a str_replace on the variable from the URL.
Code: Select all
<a href="http://www.mysite.com/<?php echo str_replace(array('%20', ' '), '-', $row_Recordset1['categorie']) . "/" . str_replace(array('%20', ' '), '-', $row_Recordset1['subcategorie']) . "/" . str_replace(array('%20', ' '), '-', $row_Recordset1['slug']) . "/"; ?>

Then add the folloign to the beginning of product.php
Code: Select all
<?php

// Replace dashes with spaces.
if(isset($_GET['categorie']))
{
  $_GET['categorie'] = str_replace('-', ' ', $_GET['categorie']);
}

// Replace dashes with spaces.
if(isset($_GET['subcategorie']))
{
  $_GET['subcategorie'] = str_replace('-', ' ', $_GET['subcategorie']);
}

// Replace dashes with spaces.
if(isset($_GET['slug']))
{
  $_GET['slug'] = str_replace('-', ' ', $_GET['slug']);
}


3. How do I rewrite the above given URL so that it automatically replaces dashes with hyphens? I want to do this so that the code of my page also contains the exact same URL with hyphens as does the rewritten URL of problem #2.

I don't understand.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby joppiesaus » Fri Oct 17, 2008 9:43 am

Graeme wrote:could you post your entire .htaccess file please?


Hi Graeme,

I fixed the image problem. I should have used relative paths starting with "/" sometimes it justed started with images/logo.jpg.

Here is my .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.nl$
RewriteRule ^(.*)$ http://www.mysite.nl/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ product.php?categorie=$1&subcategorie=$2&slug=$3 [L]

From what I understood rewriting URL's where spaces are replaced with "-" is nearly "impossible" or unwanted at the least.

Currently im linking with my own homebrewed link to different products:

<a href="http://www.mysite.com/<?php echo $row_Recordset1['categorie'] . "/" . $row_Recordset1['subcategorie'] . "/" . $row_Recordset1['slug'] . "/"; ?>

However, this gives undesireable output with spaces. So the only thing left for me to figure out is to output the variables in such a way that they link with hyphens. PHP is involved, that I understand, the rest is a riddle to me. Do you know a way?



Edit: Thanks Richard, hadnt seen your post yet. Il certainly try to get the code you said working! Ill let you know how it went.
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby joppiesaus » Fri Oct 17, 2008 2:27 pm

Hi Richard,

This totally helped me out. I can now generate the correct URL's.

One problem dough, on my product page I use a GET to get the "slug" variable to get the right information for the product. This doesnt seem to work anymre and I dont know why.

Code .htaccess:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.nl$
RewriteRule ^(.*)$ http://www.mysite.nl/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ product.php?categorie=$1&subcategorie=$2&slug=$3 [L]


Info for the GET:

Code: Select all
$colname_Recordset1 = "-1";
if (isset($_GET['slug'])) {
  $colname_Recordset1 = $_GET['slug'];
}
mysql_select_db($database_kssql, $kssql);
$query_Recordset1 = sprintf("SELECT * FROM product WHERE slug = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $kssql) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


Can you see what might be going wrong?

Also, I want to rewrite "anypage.php" to "anypage/" (so I can link directly to "anypage/"). Do you know the apache code for this?

Thanks!

Nick.








richardk wrote:
Code .htaccess:

You should use
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.nl$ [NC]
RewriteRule ^(.*)$ http://www.example.nl/$1 [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /product.php?categorie=$1&subcategorie=$2&slug=$3 [QSA,L]


1. Images wont show up aymore. What should I add or change in the .htaccess for this?

Read the FAQ i previously linked to. You do not add anything to your .htaccess file, you add a <base> to your HTML.

2. The URL still contains %20 (spaces) which I want to convert to dashes. When I manually link with dashes it does work, but there is no way to do this automatcally now because the variables in my database d not contain hyphens.

Use PHP to do a str_replace() when 0outputting the links and a str_replace on the variable from the URL.
Code: Select all
<a href="http://www.mysite.com/<?php echo str_replace(array('%20', ' '), '-', $row_Recordset1['categorie']) . "/" . str_replace(array('%20', ' '), '-', $row_Recordset1['subcategorie']) . "/" . str_replace(array('%20', ' '), '-', $row_Recordset1['slug']) . "/"; ?>

Then add the folloign to the beginning of product.php
Code: Select all
<?php

// Replace dashes with spaces.
if(isset($_GET['categorie']))
{
  $_GET['categorie'] = str_replace('-', ' ', $_GET['categorie']);
}

// Replace dashes with spaces.
if(isset($_GET['subcategorie']))
{
  $_GET['subcategorie'] = str_replace('-', ' ', $_GET['subcategorie']);
}

// Replace dashes with spaces.
if(isset($_GET['slug']))
{
  $_GET['slug'] = str_replace('-', ' ', $_GET['slug']);
}


3. How do I rewrite the above given URL so that it automatically replaces dashes with hyphens? I want to do this so that the code of my page also contains the exact same URL with hyphens as does the rewritten URL of problem #2.

I don't understand.
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby richardk » Fri Oct 17, 2008 2:37 pm

One problem dough, on my product page I use a GET to get the "slug" variable to get the right information for the product. This doesnt seem to work anymre and I dont know why.

If you var_dump() $colname_Recordset1 and $_GET['slug'] (after the IF) what are their values.

Also, I want to rewrite "anypage.php" to "anypage/" (so I can link directly to "anypage/"). Do you know the apache code for this?

Try adding
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

after
Code: Select all
RewriteRule ^(.*)$ http://www.example.nl/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby joppiesaus » Fri Oct 17, 2008 2:49 pm

Hi Richard,


weirdest thing, I deleted some of your code:

Code: Select all
// Replace dashes with spaces, herschrijven van de URL
if(isset($_GET['categorie']))
{
  $_GET['categorie'] = str_replace('-', ' ', $_GET['categorie']);
}

// Replace dashes with spaces.
if(isset($_GET['subcategorie']))
{
  $_GET['subcategorie'] = str_replace('-', ' ', $_GET['subcategorie']);
}

// Replace dashes with spaces.
if(isset($_GET['slug']))
{
  $_GET['slug'] = str_replace('-', ' ', $_GET['slug']);
}


But simply used your "dynamic URL". This works fine. I can now link with the correct link and the product page is able to "GET" the slug variable. So, do I really need this code?

The rewrite for the .php to "/" works fine, thanks! :)

richardk wrote:
One problem dough, on my product page I use a GET to get the "slug" variable to get the right information for the product. This doesnt seem to work anymre and I dont know why.

If you var_dump() $colname_Recordset1 and $_GET['slug'] (after the IF) what are their values.

Also, I want to rewrite "anypage.php" to "anypage/" (so I can link directly to "anypage/"). Do you know the apache code for this?

Try adding
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

after
Code: Select all
RewriteRule ^(.*)$ http://www.example.nl/$1 [R=301,L]
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby richardk » Sat Oct 18, 2008 3:58 am

If it works without it, you don't need it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby joppiesaus » Sat Oct 18, 2008 5:03 am

richardk wrote:If it works without it, you don't need it.


Cool :) (thought there might have been anoter reason why this was here (failsafe or something).
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Previous

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 20 guests

cron