mod_rewrite

Discussion in 'Coding Help' started by ziycon, Jan 27, 2007.

  1. ziycon

    ziycon New Member

    Is this how the code is written in the .htaccess file or how to you seperate two rules?
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs/([a-zA-Z0-9_]+)$ v/cl_desc.php?id=$1
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs-dublin/([a-zA-Z0-9_]+)$ v/cl_desc_vote.php?id=$2
    

     
  2. mneylon

    mneylon Administrator Staff Member

    You shouldn't have to repeat the REQUEST line
     
  3. ziycon

    ziycon New Member

    So i just do it like this and have as many rules as i want!?
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs/([a-zA-Z0-9_]+)$ v/cl_desc.php?id=$1
    RewriteRule ^clubs-dublin/([a-zA-Z0-9_]+)$ v/cl_desc_vote.php?id=$2
     
  4. mneylon

    mneylon Administrator Staff Member

    Pretty much :)

    If you look at this one from a wordpress blog:

    Code:
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    In that example all processing is done within wordpress

    Or for Irishblogs.com:

    Code:
    <IfModule mod_rewrite.c>
       RewriteEngine On
    
       ## Details Link Page Rewrite##
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-l
       RewriteRule (.*)detail/link-(.*).htm[l]?$ detail.php [QSA,NC]
    
       ## Pagination Rewrite
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-l
       RewriteRule (.*)page-(\d+)\.htm[l]?(.*)$  $1/?p=$2 [PT,NC]
    
       ## Category redirect
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-l
       RewriteRule ^(.*)$ index.php [QSA,L]
    
    </IfModule>
    
    They've actually repeated it ....

    In either case if you have access to your httpd.conf you can turn on mod_rewrite logging to see what is working (or not as the case maybe)
     
  5. ziycon

    ziycon New Member

    Nice one, i just cant seem to get it working! the first rule is working fine but the second one keeps bringing me to an error page saying that that page im looking for doesnt exist! :mad:

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs/([a-zA-Z0-9_]+)$ v/cl_desc.php?id=$1
    RewriteRule ^clubs-dublin/([a-zA-Z0-9_]+)$ v/cl_desc_vote.php?id=$2
    It says for the second like that the url is clubs/clubs-dublin/1 is it ment to take the first rule and add it in like that!?
     
  6. daviddoran

    daviddoran New Member

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs/([a-zA-Z0-9_]+)$ v/cl_desc.php?id=$1
    RewriteRule ^clubs-dublin/([a-zA-Z0-9_]+)$ v/cl_desc_vote.php?id=$1
    
    The second Rule should have $1, not $2 as I have changed.

    Also, Apache doesn not cascade the rules (add them), as soon as one matches it will redirect.
     
  7. ziycon

    ziycon New Member

    Anyone know how to setup mod_rewrite on a local enviroment, there is no .HTAcess file, on the htdocs apache folder!?
     
  8. mneylon

    mneylon Administrator Staff Member

    Is the module available / installed?
     
  9. ziycon

    ziycon New Member

    yes, its enabled and all, im not sure where to put the rules for it!?
     
  10. mneylon

    mneylon Administrator Staff Member

    Two options:

    1 - in the apache vhost config of the test site
    2 - in a .htaccess file
     
  11. ziycon

    ziycon New Member

    so i jus create a .htaccess file and put it into the htdocs folder or htdocs/wesbite/ folder?
     
  12. mneylon

    mneylon Administrator Staff Member

    I don't know how you've configured your setup, but I would presume that the web root for the site is in htdocs/wesbite/ , so you should put it in there ie. it has to be part of the actual site
     
  13. ziycon

    ziycon New Member

    ok, thanks, i'll try that.
     
  14. ziycon

    ziycon New Member

    Keep getting this error:
    This what i have in the .htaccess file:
    Code:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^clubs/([a-zA-Z0-9_]+)$ v/cl_desc.php?id=$1
    RewriteRule ^clubs-dublin/([a-zA-Z0-9_]+)$ v/cl_desc_vote.php?id=$1
     
  15. mneylon

    mneylon Administrator Staff Member

  16. markowe

    markowe New Member

    Are you sure your hosting service fully supports .htaccess? Stupid question, but not all of them do, and you can get errors like that for that reason.

    That's one of the reasons why I moved from Netfirms...
     
  17. daviddoran

    daviddoran New Member

    AFAIK if mod_rewrite was not available then you would get a 500 Error so that is ok.
    More Obvious Questions:
    1) Is the .htaccess script in the dn directory?
    2) Is v/cl_desc_* relative to the .htaccess file?
     
  18. markowe

    markowe New Member

    Not necessarily - on Netfirms hosting, the .htaccess seems to work, but the rewrites don't work, and just direct you back to index.php. No 500 errors... Just a suggestion, because it has been the cause of many a frustration with .htaccess (and a good reason to change provider in my book).
     
  19. ziycon

    ziycon New Member

    1) yes
    2) yes

    Its on a localhost, its a test machine at home!
     

Share This Page