• Hi there,

    I know, this is another rewrite post but i tried everything that i cuold find on the forums.

    WordPress is setup with permalinks, the settings are:
    custom: /%category%/%post_name%/

    Ive added a page on my site and named it products, in this page i inlcude a php file that processes the query string.
    The query string is actualy quit simple and has only one variable: categorie

    I want to accomplish that this url:
    http://mydomain/products/something/ ==> http://mydomain/products/?categorie=something

    I tried to add a rule directly in tha htaccess file:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^products/(.*)/ products/?categorie=$1 [L]
    
    #uploaded files
    RewriteRule ^(.*/)?files/$ index.php [L]
    RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
    RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteCond %{REQUEST_URI} ^.*/wp-admin$
    RewriteRule ^(.+)$ $1/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule . - [L]
    RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
    RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]
    
    <IfModule mod_security.c>
    <Files async-upload.php>
    SecFilterEngine Off
    SecFilterScanPOST Off
    </Files>
    </IfModule>

    This resulted in a 404 or when i replace ‘something’ with a page or post name that exists that page or post is shown.

    The second thing i tried was a plugin:

    function my_rewrite_rules( $rewrite_rules ) {
    
        $new_rules = array( '^/producten/(.*)/' => '^/products/?itemId=$1');
    
        $rewrite_rules = $new_rules + $rewrite_rules;
    
        return $rewrite_rules;
    
    }
    
    add_filter('rewrite_rules_array', 'my_rewrite_rules');

    Afther isntallation i flushed the current rules by re-saving my permalink setting.
    The results where the same, 404 or when ‘something’ is an existing page or post name, that one will show up.

    Please give me some pointers. My frustration is getting a solid form 😛

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi,

    Set your desired permalink and add this code in htaccess:

    # BEGIN WordPress
    
    <IfModule mod_rewrite.c>
    ErrorDocument 404 /index.php?error=404
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Also remove all old code for wordpress.

    Thanks,

    Shane G.

    Thread Starter snapnie

    (@snapnie)

    this doesn’t solve my problem. the rewrite i need still isnt working.

    Did you print out the rewrite rules array after adding them to make sure they look ok and they are present?..

    // global $wp_rewrite;
    print '<pre>';
    print_r( $wp_rewrite->rules );
    print '</pre>';

    NOTE: If that doesn’t print anything, uncomment the top line..

    I’ve used quite a few custom search rules without a problem, using a similar approach to your “my_rewrite_rules” function above.

    You’ll need to save your permalink settings after any changes when using a filter like above.

    Thread Starter snapnie

    (@snapnie)

    That is actualy some nice info. I get all the rules and i see that my rules added at the beginning of the array.
    I suspect that my rules are being overwritten by another rule, so is it possible to add my custom rules at the end of the array?

    Ah, i’ve just realised something, and i’m not sure if this will apply, but it’s worth mentioning..

    Try changing this..

    array( '^/producten/(.*)/' => '^/products/?itemId=$1')

    for

    array( '^/producten/(.*)/' => '^/products/?itemId=$matches[1]')

    I had problems using the $1, $2 etc back references, but using $matches[n] seemed to work fine..

    See if that helps at all.

    Thread Starter snapnie

    (@snapnie)

    Ok, so i tried a few more things.

    With WP_Rewrite i can add the following rules to the databse:

    ^producten/([0-9]+)$/ => producten/?itemId=$matches[1]

    This rule is shown when i print the list (WP_Rewrite->rule).

    When i request this url: http://mydomain/producten/24/ the $_GET[] is empty.
    But, when i add hte same rule in the htaccess file $_GET[] prints:
    itemId => 24

    So one of my rules is working in the htaccess file.

    This is another rule i need:
    ^producten/(.*)$/ producten/?categorie=$matches[1]
    This rule is also shown in the database when i call WP_Rewrite-> rules.
    But on a request i get two possible responses:

    http://mydomain/producten/thirts/ (tshirts is an existing page)
    the respond is the original tshirt page.

    http://mydoamin/producten/foobar/ (just some text)
    the respond is a 404

    When i add this rule in the htaccess file it’s the same responses on the above requests.

    Is this normal behavior or should i disable all plugin’s (perhaps All in one SEO)?

    If you have an existing page with the name “producten”, your rules are going to conflict..

    If you want to use your own rules for “producten”, then you’ll need to either rename your page permalink or remove it.

    This is precisely why pages should be used sparingly for static content only, each page has at least a few (can have many if child pages exist) sets of rewrite rules , different to how post permalinks work.

    RE your rule:

    ^producten/(.*)$/
    What will be queried (The URL that's going to be queried)
    
    producten/?categorie=$matches[1]
    Where the match will be sent (not the URL you'll query, but where to send the request)

    I believe you need to determine where you should passing these matches to.
    Example:

    producten/index.php?categorie=$matches[1]

    In the above matches would be passed to the index.php in that directory (assuming producten is a directory). Without such information i don’t think your rule knows where it’s sending the variables to…

    TIP: Try to avoid using (.*) in your rules, this is highly inefficient for apache. If you’re expecting at least 1 character use (.+) or ([^/]+) , the first matches any single character, the second matches any single character that’s not a forward slash..

    Thread Starter snapnie

    (@snapnie)

    hi t31os_,

    thanks a lot for this information. This gives me some new things i can try.
    I will post my findings..

    again ty

    No problem, you may also be interested in taking a look at the apache docs for mod_rewrite..
    http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule
    And also some regex (even if only for some basic understanding if needed), as it’ll help with the syntax for rewrite rules…
    addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

    I was thinking of doing something similar and came here looking for info on rewriting.

    However, after reading Creating a (Better) Fake Post with a WordPress Plugin, I think I’ll try that instead. Rather than rewriting, just check the URL and if it starts with the stem of the URL you’re using, hijack it and create a fake page. Shouldn’t be too hard to extract the arguments from the requested URL and serve up a different fake page depending on their contents.

    I haven’t tried this yet, but is seems to be a viable alternative to rewriting.

    I’m still trying to figure out how to ensure that existing directories are stills accessible with custom htaccess paths. The htaccess code works for txt files, but not for php – not sure why!

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    —-
    online tv

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Completely stuck on rewrite rules’ is closed to new replies.