URL rewriting dynamic URL of my webshop.

Discuss practical ways rearrange URLs using mod_rewrite.

URL rewriting dynamic URL of my webshop.

Postby joppiesaus » Tue Oct 14, 2008 11:33 pm

Hello,

I am currently working on a new webshop at which i want to rewrite a dynamic URL.

Currently the UL looks like this:

http://www.mysite.nl/product.php?catego ... uper%20hat

I want to make it look like this:

http://www.mysite.nl/hat/red-hat/super-hat/

However, I cannot get this to work!

I currently have this code running which always makes sure the URL starts with "http://www."

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.nl$ [NC]
RewriteRule ^(.*)$ http://www.mysite.nl/$1 [R=301,L]

I want to use this code to rewrite the URL:

RewriteRule http://www.mysite.nl/(.*)/(.*)/(.*)/ product.php?categorie=$1&subcategorie=$2&slug=$3


So the .htaccess file looks like this:


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


However, this doesn't seem to work. Can anybody help me?
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby richardk » Wed Oct 15, 2008 6:45 am

The RewriteRule matches everything after the current directory that the .htaccess file is in and up to the query string.
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]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby joppiesaus » Wed Oct 15, 2008 7:14 am

richardk wrote:The RewriteRule matches everything after the current directory that the .htaccess file is in and up to the query string.
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]


Hello Ricardk,

Thanks for the help, however, this doesnt have the desired effect. Th URL is in no way rewritten, but all the images (which are in /images/site/) error out. Or simply said, they ont load with this commandline in the .htaccess file.

It did have some effect, so I guess thats positive :). Do you know what went wrong?
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby richardk » Thu Oct 16, 2008 6:42 am

Th URL is in no way rewritten

What do you mean? Mod_rewrite does not change the URLs outputted by your script.

all the images (which are in /images/site/) error out.

FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby joppiesaus » Thu Oct 16, 2008 9:43 am

richardk wrote:
Th URL is in no way rewritten

What do you mean? Mod_rewrite does not change the URLs outputted by your script.

all the images (which are in /images/site/) error out.

FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.


shouldnt the URL change to mysite.com/hat/red-hat/super-hat/ ??

Now nor the URL in the browser, nor he URL in the outputten source code is altered.. I was under the impression that the URL in the browser would be rewritten..
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby Graeme » Thu Oct 16, 2008 2:42 pm

if you are expecting it to do automatically then it wont.

just time in your expectant nice url and see if that works
Graeme
 
Posts: 9
Joined: Thu Sep 11, 2008 6:43 am

Postby joppiesaus » Fri Oct 17, 2008 1:01 am

Graeme wrote:if you are expecting it to do automatically then it wont.

just time in your expectant nice url and see if that works


Hi, well, I got it working (I just misunderstood on how it should work).

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


When I now link to:

mysite.com/hat/red hat/super hat/

The right page and info turns up as if I had entered the dynamic URL (Huray! :) )

This still leaves me with 3 problems:

1. Images wont show up aymore. What should I add or change in the .htaccess for this?
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.

I now link to each page using this URL:
<a href="http://www.mysite.com/<?php echo $row_Recordset1['categorie'] . "/" . $row_Recordset1['subcategorie'] . "/" . $row_Recordset1['slug'] . "/"; ?>
So how do I rewrite the URL with dashes?
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.

Hope you can help!
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby Graeme » Fri Oct 17, 2008 5:59 am

Code: Select all
RewriteCond %{REQUEST_URI}    !^/images/.*


will allow your images to be used
Graeme
 
Posts: 9
Joined: Thu Sep 11, 2008 6:43 am

Postby joppiesaus » Fri Oct 17, 2008 6:18 am

Graeme wrote:
Code: Select all
RewriteCond %{REQUEST_URI}    !^/images/.*


will allow your images to be used


Hi Graeme,

relative paths still do not show up correctly. Their paths are now for instance like this on the product page:

/hat/black hats/super hat 1000/images/site/logo.jpg

while they should be:

/images/site/logo.jpg

Hope this helps.
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby Graeme » Fri Oct 17, 2008 7:41 am

could you post your entire .htaccess file please?
Graeme
 
Posts: 9
Joined: Thu Sep 11, 2008 6:43 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 5 guests

cron