url / slugs / permalinks help!

Ok, so this is my third day trying to solve this… I’m probably doing it wrong since any solution i find our there in existing posts are just not working for me… WHAT am I doing wrong!! >:(

Ok so here is the deal…
I have rather ugley URLS on my website right now and I would like to make them a bit prettier!

An example of my url today…
http://www.mydomain.com/page.php?product=2

I want it to look like this:
http://www.mydomain.com/product-name/

I just cant figure out how to solve this… And now on the third day I’m almost ready to give up :frowning:
But hey, I won’t! :wink: Need to hit that deadline!

So can someone give me a hint on where to start, how to think, the basics…
Do I need php to write this as well or can all this be done with only playing in the htaccess file?

Appreciate all the help I can get :slight_smile:
Best regards, T

There are two things you need to do to implement this. If you have product database:

  1. Add url rewrite rule to your .htaccess file. This should work for you:
    .htaccess

[code]RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?productname=$1 [L][/code]

  1. In your page.php script, use value of $_GET[‘productname’] to determine product.

If you have no database and just have few products, just set rule for every product in .htaccess like this

[code]RewriteRule ^productname1$ page.php?product=1 [L]
RewriteRule ^productname2$ page.php?product=2 [L]

etc.[/code]

Thank you!
This really helped and your code works like a charm, and I got it all hooked up with my $_GET variable! :slight_smile:
A quick question though…

I guess I’m suppose to use the new URL in my actual hrefs on my site. Souch as:
view product

But…

Is there no way of keeping the original one:
view product

And having the htaccess file see this, go there and change the url to /product/the-product-name/
So I don’t have to go back and change all the hrefs on my site :slight_smile:

Or is it bad to use redirection like that in a SEO manner?
Best regards, T

Your original link should work too. See in rewrite rules in the .htaccess we’ve specified these conditions:

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f

they mean: “if this is not a directory and not a file in local filesystem rewrite this to…”

So, if your original links were: index.php?productname=the-product-name
they should still work fine, because index.php is a file (so our rewrite rule does not apply)

But I’d suggest to change all links to new, or at least have them redirect 301 to new (otherwise, these 2 different urls will look as duplicate pages for search engines)

Sponsor our Newsletter | Privacy Policy | Terms of Service