Is this Mod Rewrite Possible?

Discussion in 'Apache' started by martinvidic, Nov 7, 2011.

  1. #1
    Hi everybody.
    I was wondering if a rediret like this can be done with mod rewrite.

    I have
    /listing.php?id=434&?name1=Sepp&?name2=Maier&?city=Boston&?state=Mass&?country=U.S.A.

    and it should be
    /sepp_maier_boston_mass_usa

    So I'm basically getting rid of the php file and the ID, all the special characters including the DOT's in U.S.A and use no capital letters are used. Can this be done? and if yes, HOW :)

    Thanx
    M.
     
    martinvidic, Nov 7, 2011 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    It pretty much depends on your php script. Rewrite rule can be simple, but it depends how your php script matches that inputted data with the data it has stored. If it's matching data with some mysql table, then capitalisation of letters is not going to be the problem, as mysql doesn't make difference between caps and no-caps (at least with default configuration of mysql), though part with removing dots from u.s.a. may actually be the problem. Also, if your script needs ID to match that product (or whatever it is) then you will have to have ID in your url as well, but just in some prettier way maybe.

    If you do find some fix for the u.s.a. part then something like this may apply:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([^/]+)_([^/]+)_([^/]+)_([^/]+)_([^/]+)$ /listing.php?name1=$1&name2=$2&city=$3&state=$4&country=$5 [L]
    
    Code (markup):
    This is all assuming that your listing.php script actually doesn't need an ID field input. Though I doubt... so you may end up with something like this instead: /434_sepp_maier_boston_mass_usa so then the rule would be:
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([^/]+)_([^/]+)_([^/]+)_([^/]+)_([^/]+)_([^/]+)$ /listing.php?id=$1&name1=$2&name2=$3&city=$4&state=$5&country=$6 [L]
    
    Code (markup):
    So, your listing.php script should be working ok with this rule, if you manage to bypass on some other way dots in the country field (possibly change your site data from u.s.a. to just plain usa) and also assuming that it matches data with mysql data. If not, then you will have to compare inputted data and your script data all in lower case, so it could match without CAPS letter interference.

    I hope this helps.
     
    Last edited: Nov 7, 2011
    pr0t0n, Nov 7, 2011 IP
  3. johristov

    johristov Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, I would like to ask a question, since I am not into this stuff...
    I have this url:
    www.izpitite.com/catalog.php?id=343

    I wanted to make url like www.izpitite.com/catalog/name-of-the-file/, but I am nowhere

    I have tried this
    RewriteEngine On # Turn on the rewriting engine
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^catalog\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /catalog.php?id=$1 [L]
    </IfModule>
    , but still nothing. I will be very grateful if you can help...
     
    johristov, Nov 10, 2011 IP
  4. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #4
    Well... if you have a limited number of products in your catalog, then you can list each one in your mod rewrite rules to have a better looking urls. For example:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^catalog/name-of-the-file/$ /catalog.php?id=31231 [L]
    RewriteRule ^catalog/name-of-other-file/$ /catalog.php?id=3213 [L]
    RewriteRule ^catalog/name-of-the-third-file/$ /catalog.php?id=434 [L]
    
    Code (markup):
    However if you have a large number of such catalog entries, or you change them often, then you can't enter each one in your .htaccess file and you need more general rule for all catalog entries. The url would have to contain your ID in this case, so something like this may apply:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^catalog/([0-9]+)-([^/]+)/$ /catalog.php?id=$1 [L]
    
    Code (markup):
    In this case your URL should look slightly different from what you asked. It should look like this: izpitite.com/catalog/343-name-of-the-file/ (343 is just an example ID you also used). So each rewritten url must contain real ID as well, so your catalog.php can know which ID you are actually requesting. The other part of the url (-name-of-the-file) is just for decoration in this case, so you can write anything you want there.

    You could also change this to look a bit different, or position ID on some other spot (at the end of url, or something like that), but you will have to list it somewhere in your url.
     
    pr0t0n, Nov 10, 2011 IP
  5. johristov

    johristov Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much for your help. I put the code in the .htaccess but nothing changed. I don't underatsnd what the reason could be. It might be because teh domain is a addon domain or maybe there is some restriction...I put in the .htaccess of the directory of this site, not the hosting root. I don't know what it could be... Any suggestions?
    Thanks in advance
     
    johristov, Nov 10, 2011 IP
  6. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #6
    It could be a few things...

    1. Your hosting company doesn't not support mod_rewrite (?)
    2. You have put the code in the wrong folder (should be the root html folder for that specific domain name, so if it is just an addon then in it's main html folder and not the account one)

    I don't see other reasons...
    Post maybe an example of such url you have that doesn't work, as well as your mod_rewrite rule that you pasted in your .htaccess (in the case you changed the example I wrote).
     
    pr0t0n, Nov 10, 2011 IP
  7. johristov

    johristov Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks Proton, I will contact hosting provider in order to clear out things. Thanks for your efforts.
    Regards,
     
    johristov, Nov 10, 2011 IP