Why oh why won't this mod_rewrite rule work?

Discussion in 'Apache' started by chemoul, Aug 27, 2006.

  1. #1
    Hi,
    I've been working on the following mod rewrite rules:
    RewriteEngine on
    Options +FollowSymlinks
    RewriteCond $1 !index\.php$
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&subpage=$2 [L]
    #
    RewriteCond $1 !index\.php$
    RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
    Code (markup):
    what it's supposed to be doing is rewrite urls, like:
    http://domain.com/article1 to http://domain.com/index.php?page=article1
    and http://domain.com/article1/subarticle to
    http://domain.com/index.php?page=article1&subpage=subarticle

    This works (sort of).
    If I load a page by clicking on a link (for example: http://domain.com/article1) the correct page loads (i.e. http://domain.com/index.php?page=article1) BUT the URL that appears in the address bar of the browser reads: http://domain.com/article1/?page=article1

    I have triple checked and there is nothing in my php script that would add this, so I'm assuming that the rewritten url gets passed through the mod rewrite rules again.

    If anyone has a solution I would be really greatful as I've been stewing over this problem for quite some time now.

    Thanks.

    Stefan
     
    chemoul, Aug 27, 2006 IP
  2. fscripting

    fscripting Banned

    Messages:
    288
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this buddy

    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule index/(.*)/(.*)/(.*)/(.*)/$ /index.php?$1=$2&$3=$4
    
    Code (markup):
    it will make it like this

    yourdomaindomain.com/index/page/article1/subpage/subarticle/
     
    fscripting, Aug 27, 2006 IP
  3. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #3
    
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]
    RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /index.php?page=$1&subpage=$2 [L]
    </IfModule>
    
    Code (markup):
    I hope this works I just wrote it up quick.
     
    smatts9, Aug 27, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #4
    Nintendo, Aug 28, 2006 IP
  5. chemoul

    chemoul Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    thank you for all of your replies. I'm afraid that none of the above suggestions would work, as the parameters are also real directory names.
    Apparently there is a clash between mod_rewrite and mod_dir, which results in the funny url "?page=article1" being added at the end of the URL.
    Modifying the directory names is not really an option, so if anyone has an idea on how to "untangle" mod_rewrite and mod_dir....

    Nintendo: I'm actually using absolute URLs in my code, e.g. http:// localhost/textcms/phitetest (the base URL) and http:// localhost/textcms/phitetest/article1/subarticle1

    (space between http:// and the rest of the url because I don't have access to live links here yet)

    Thanks.

    Stefan
     
    chemoul, Aug 30, 2006 IP
  6. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #6
    If you plan to ever have any visitors, http:// localhost/ won't work at all.

    http://www.domain.com is what you want.

    View the HTML code of the page and see what you get in the link.
     
    Nintendo, Aug 30, 2006 IP
  7. chemoul

    chemoul Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks Nintendo, but that's not the issue here. I'm using a local development server, which is why it is localhost.

    The problem is that no mod rewrite rule seems to work because of the conflict between mod_rewrite and mod_dir I described above.

    Thanks.

    Stefan
     
    chemoul, Aug 30, 2006 IP