Development Mod Rewrite Problem

Discussion in 'Software' started by Stuey, 31 Dec 2008.

  1. Stuey

    Stuey You will be defenestrated!

    Joined:
    20 Jan 2005
    Posts:
    2,612
    Likes Received:
    10
    Okay, so I'm trying to redirect .../page/category to /page.php?id=category.

    I put this code into the htaccess file of one domain, and it worked.
    Code:
    RewriteEngine On
    RewriteRule ^page/([/_0-9a-zA-Z-]+)$ linp.php?id=$1 [L]
    My page.php file was also coded properly.

    Now, when I went to implement this code in a domain where Wordpress is installed, the rewrite rule breaks. No matter what I try, the Wordpress rewrites break my rule!

    Wordpress htaccess code:
    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
    How do I alter my rewrite rule so that it is not affected by the WP rewrites?

    Thanks.
     
  2. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    I think if you add your rule just before WordPress's "RewriteRule . /index.php [L]" it should work. The [L] flag should tell it to ignore further rules if it executes that one, so yours will need to come first.

    Are these pages separate from Wordpress? By default it uses a /category/name rewrite IIRC so they could be getting angry at each other somehow.
     
    Stuey likes this.
  3. Stuey

    Stuey You will be defenestrated!

    Joined:
    20 Jan 2005
    Posts:
    2,612
    Likes Received:
    10
    I am doing the testing in a domain that does not have wordpress installed, so there isn't any conflict related to that.

    I think that I'm getting a handle on things. The code seems to work fine if Rewrite Base / is excluded entirely, but then things break down when it's put back in.

    Now when I rewrite in this manner:

    Code:
    #
    RewriteEngine On
    RewriteRule ^page/([/_0-9a-zA-Z-]+)$ black.gif [L]
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    navigating to page/anything now displays the image. This now suggests that my php file needs to be edited to allow things to work properly.

    There's also a catch - the wordpress code is rewritten by Wordpress, so I have to keep my rewrites above or below it.

    This has been driving me nuts all day - I really appreciate the advice! I tried placing my rewrite above, below, and in between the WP code, but your comment made me swap the code order and the rewrite destination.

    Something else has been bugging me as well. If I don't include the # or a space before the content of the htaccess file, the contents of the entire file is ignored! So you can imagine, this hasn't been a fun afternoon and evening for me!

    Edit: Nevermind. Still encountering difficulties.

    This is the code that I'm trying to redirect to, in a file called 'test.php'

    Code:
    <?php
    $path = array(
    	'test1' => 'http://www.google.com',
    	'test2' => 'http://forums.bit-tech.net'
    	);
    $home='/';	
    if (array_key_exists($_GET['id'],$path))
    	header('Location: ' . $path[$_GET['id']]);
    else header("Location:$home");
    ?>
    navigating to /test.php?id=test1 or /test.php?id=test2 redirects me to the respective pages.

    After replacing "black.gif" with test.php?id=$1 as the destination page, /page/test1 and /page/test2 do not rewrite as intended, and I only get 404 errors.

    Again, removing the RewriteBase / line fixes everything. But, Wordpress will reinsert it automatically unless I go into the Wordpress backend and start editing things there, which might cause even more problems. I don't even want to remind myself that this is all on a clean domain and that if/when this is settled, I have to then implement everything into the domain which does have WP installed...
     
    Last edited: 31 Dec 2008
  4. Stuey

    Stuey You will be defenestrated!

    Joined:
    20 Jan 2005
    Posts:
    2,612
    Likes Received:
    10
    Eh, I think I'll just avoid the mod rewrite and just live with the fact that I'll have to link to .../page.php?id=link, instead of .../page/link.

    Oh wells.
     
  5. Stuey

    Stuey You will be defenestrated!

    Joined:
    20 Jan 2005
    Posts:
    2,612
    Likes Received:
    10
    I was able to circumvent the problem by creating a folder called links, and then throwing htaccess redirect in there. yippie!

    so now .../test/test1 redirects to test.php?id=test1, and I'm as happy as could be.
     

Share This Page