mod_rewrite won't work

Discussion in 'Apache' started by jonattree, Feb 16, 2009.

  1. #1
    Hi
    I want to use mod_rewrite with .htaccess file to clean up my URLs so that for example, this:

    www.mysite.com/artist.php?cid=12&aid=56

    displays as this:

    www.mysite.com/artist/12/56

    I have enabled
    LoadModule rewrite_module modules/mod_rewrite.so
    in my httpd.conf file and placed the .htaccess file in the site root.
    The .htaccess file contains this:

    RewriteEngine On
    RewriteRule ^artist/(.*)/(.*)/ /artist.php?cid=$1&aid=$2

    I have full control of my development server but no control over production server.

    When I try to load www.mysite.com/artist/12/56 I get a Not Found error:
    'The requested URL /bigartweb/artist/12/56 was not found on this server.'

    Am very new to Apache so any help much appreciated.

    Jonathan Attree
     
    jonattree, Feb 16, 2009 IP
  2. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try putting this "RewriteBase /" under RewriteEngine on.. like this;

    RewriteEngine on
    RewriteBase /

    I had the same issue a little while back and this seemed to fix it.
     
    websecrets, Feb 17, 2009 IP
  3. jonattree

    jonattree Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. Had already tried that in the meantime. My .htaccess file now looks like this but still doesn't seem to work:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^artist/(.*)/(.*)/ /artist.php?cid=$1&aid=$2 [L]

    Am using Apache 2.2 on WinXP Pro and my site root is at:
    C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\bigartweb
     
    jonattree, Feb 17, 2009 IP
  4. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Where are the pages going now?

    Try...

    RewriteRule ^artist/(.*)/(.*)$ /artist.php?cid=$1&aid=$2 [L]
    or
    RewriteRule ^artist/(.*)/(.*)/$ /artist.php?cid=$1&aid=$2 [L]
     
    websecrets, Feb 17, 2009 IP
  5. jonattree

    jonattree Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry, neither of those work either.
    I currently have only two pages:
    C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\bigartweb\index.php and
    C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\bigartweb\artist.php
    I'm trying to call artist.php from index.php.

    If I change the rule to this:
    RewriteRule ^artist.php /test.php [L]
    It should redirect artist.php to test.php, yes? This doesn't work either so maybe mod_rewrite is not working at all?

    Thanks again
    Jonathan Attree
     
    jonattree, Feb 18, 2009 IP
  6. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #6
    The rule should be:
    RewriteEngine on
    RewriteRule ^artist/([^/]*)/([^/]*)/$ artist.php?cid=$1&aid=$2
    
    Code (markup):
    However the URL must be www.mysite.com/artist/12/56/ note the trailing / www.mysite.com/artist/12/56 will not match the rule, if you want either to work then use:
    RewriteEngine on
    RewriteRule ^artist/([^/]*)/([^/]*)/?$ artist.php?cid=$1&aid=$2
    
    Code (markup):
    If that doesn't work then check that mod_rewrite is loading. I don't ever run Apache on Windows so am just assuming it works the same as on Linux.
     
    tolra, Feb 18, 2009 IP
  7. jonattree

    jonattree Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for reply.
    This now works and passes correct values to called page:

    Options +Indexes
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^artist/([^/]*)/([^/]*)/$ artist.php?cid=$1&aid=$2

    However now all relative links to images, stylesheets etc fail on called page.
    If I call mysite.com/artist/12/34/ this translates ok to mysite.com/artist.php?cid=12&aid=34
    but a link back to index.php from artist.php then appears in the status bar as:
    mysite.com/artist/12/34/index.php.

    Any more help please?

    Thanks in advance.

    Jonathan Attree
     
    jonattree, Feb 18, 2009 IP
  8. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #8
    In the <head> area add <base href="http://www.yourdomain.com/" /> changing that to be the absolute path to your site.
     
    tolra, Feb 18, 2009 IP
  9. jonattree

    jonattree Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks tolra, it now works OK.
    Only problem left is that I now have to change the <base> tag every time I upload files from testing to live server.
    No way around that I suppose?

    Thanks again.
     
    jonattree, Feb 19, 2009 IP
  10. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #10
    Put the header in a server side include, or use PHP or use a CMS, any should make it easier.
     
    tolra, Feb 19, 2009 IP