Hi!
I'm currently developing a website and I'd like to rewrite the URLs with htaccess. I've looked up some tutorials on how to do it, but it doesn't seem to work. This is one of the url's :

products.php?sub=997&id=97084&name=Manual-Control

I'd like the link to become:

products/997/97084/Manual-Control

This is my current .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*) products.php?sub=$1&id=$2&name=$3

The problem is that the URL doesn't get rewritten. I uploaded the .htaccess file in the public-html folder (where products.php is)

Any help on this? Thanks in advance!

Recommended Answers

All 12 Replies

.* is greedy, so it will take too much, most likely. You can try:

RewriteRule ^(\d+)/(\d+)/(.*)$ products.php?sub=$1&id=$2&name=$3 [L]

Assuming the first two will only contain digits. If that also does not work, try the simplest first, something like this:

RewriteRule ^test$ products.php [L]

It should rewrite http://examples.com/test to http://example.com/product.php. If that does not work either, contact your host to see if mod_rewrite is enabled.

RewriteRule ^(\d+)/(\d+)/(.*)$ products.php?sub=$1&id=$2&name=$3 [L]

Doesn't work, and

RewriteRule ^test$ products.php [L]

does, because entering /test brings me to /products.php, so I assume mod_rewrite is enabled..

What am I doing wrong?

EDIT: The first and second rules CAN contain both letters and numbers from time to time

How about with a single first:

RewriteRule ^(\w+)$ products.php?sub=$1 [L]

Then try with two:

RewriteRule ^(\w+)\/(\w+)$ products.php?sub=$1&id=$2 [L]

That didn't work either.
What I did just notice is that when I add

RewriteRule ^test$ products.php [L]

The page that'll be displayed is products.php but the URL will still say "www.blabla.com/test" and not "www.blabla.com/products.php"

That's the point of the rewrite. If you want a real redirect, add [R=301]

hmm OK I guess that was pretty stupid of me :D
Is there anything else I can try? :/

OK, the htaccess I'm now using seems to return everything from my db :

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^products/([^/]*)/([^/]*)/([^/]*)$ /products.php?sub=$1&id=$2&name=$3 [L]

so /products/P97/p97084/PSE-Manual-Control retrieves everything from the database through mysql, but the problem now is that my CSS file and images aren't loading. I guess this is because the browser thinks it should get the CSS file from the folder /products/ ..
Any help with this?

EDIT: It thinks the folder is /products/p97/ now..

Make sure you use absolute links.

Are absolute links a standard in web development or is there just no way to get around them?

Since your file now appears to be in a folder, you need a way to open it. Absolute path is sure to work. I don't make claims about standards, but I prefer absolute links.

Since your file now appears to be in a folder, you need a way to open it. Absolute path is sure to work. I don't make claims about standards, but I prefer absolute links.

OK, I changed every link to an absolute one and got everything working. Weird thing is, my php include couldn't open the absolute link so I had to keep the relative link.

Thanks for helping me!

Yes, true. For HTML/CSS starting with a backslash means the www root, and for PHP it means the root of the current volume.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.