1

I've been reading a few tutorials online and searched a couple of other questions here but I cant seem to grasp the re-writing of dynamic URLS using the HTACCESS file.

I have URLs like:

products.php?cat=2
products.php?cat=3
products.php?cat=4

I would like them to just say products/2 for example

equipment.php?cat=2&subCat=1
equipment.php?cat=2&subCat=2
equipment.php?cat=2&subCat=3

I would like these to say equipment/2/1 for example

product.php?id=3010-Z89CH24

I would like these to say product/3010-Z89CH24 for example

but so far just trying to change the products.php pages I have this:

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

Options +FollowSymLinks

RewriteRule ^products/'^([1-9][0-9]{0,2})'$ http://www.*******.com/products.php?cat=/$2 [L] 
# REMOVE PHP EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

the thing is I dont think the end part "/$2" is correct and im unsure of the rest of it too :/

Either way its not working for me and I am stuck and struggling to understand it :(

can someone help me please?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Kevlar
  • 344
  • 1
  • 7
  • 25

3 Answers3

1

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)&subcat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /$1.php?cat=$2&subcat=$3 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This has gotten me further than I was before but for some reason the page loads without any styling and a lot of content missing? any idea why? – Kevlar Sep 26 '13 at 14:13
  • You have hit the most common problem people face when they switch to pretty URLs :) FIx is simple also, just make sure to use absolute paths for js, css, images etc. Path needs to start with `/` or `http://` – anubhava Sep 26 '13 at 14:39
  • thankyou :) that does work...but I have to use absolute paths throughout the entire site? this would take a lot of work the site is quite big – Kevlar Sep 26 '13 at 14:50
  • just an update. I found that if I put " " into the section of my site that sorts out my problem. Luckily for me all my pages are in the root directory so that base URL works :) – Kevlar Sep 26 '13 at 14:58
  • I actually spoke to soon :/ i did a quick clear out of cache and cookies etc and my pages went back to loading without styles...even when I use an absolute path to the style sheets etc – Kevlar Sep 26 '13 at 15:23
  • Check updated code and retest with absolute paths for css, js etc. – anubhava Sep 26 '13 at 15:32
  • @anubhava i am have the same issue i am working on localhost and my path is this http://localhost/demo_work/2/products.php?cat=2 how i can make http://localhost/demo_work/2/products/2 (it is showing 404) i have put the .htaccess in floder name 2.(as i can not post new question there for i am asking on comment pls help me ) – Ajit Singh Mar 22 '14 at 15:21
  • @hunter: You should be able to post a question here anytime. It is difficult to post code in comments otherwise. – anubhava Mar 22 '14 at 16:29
  • @anubhava sir i know i have to post question but stackoverflow block question from my account. that is why i asked your help in comment. now u tell me what should is do. – Ajit Singh Mar 22 '14 at 16:40
0

hope this could help you...

http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

URL rewriting with PHP

Community
  • 1
  • 1
anbusekar
  • 785
  • 2
  • 6
  • 6
  • already found that top link before I came here. unfortunately although their explanations appear simple. they are not working for me. obviously im doing something wrong but i dont know what that is :/ – Kevlar Sep 26 '13 at 14:24
0

Your problem is that your regexp is wrong - I'd recommend checking out some kind of basic regular-expression tutorial, paying attention to mentions of capture groups.

Without checking - and assuming that your digit-based-restrictions are correct - I'd guess you want a rule something like this:

RewriteRule  ^/products/([1-9][0-9]{0,2})$  /products.php?cat=$1 [L]

If you need to preserve existing query-strings you'll probably want the QSA flag too.

nickgrim
  • 5,387
  • 1
  • 22
  • 28
  • Hi, I could not get anywhere with this. trying different regex in there but it just gives me internal server errors – Kevlar Sep 26 '13 at 14:20