• Hey guys,

    I dont find something similar on the web. I’ve created a page called /gallery/ in the backend that uses the permalink settings of wordpress.

    What I’m trying to do now is to add something behind it like /gallery/Portfolio-Itemthat has to lead to /gallery/?title=Portfolio-Item without changing url.

    That is my current htaccess file:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /theme/
    RewriteRule ^gallery/([^/]+)/?$ /theme/index.php?page_id=9&title=$1 [NC,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /theme/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /theme/index.php [L]
    </IfModule>
    # END WordPress

    Now if I’m trying to use …/theme/gallery/Portfolio-Item I’m getting redirected to /gallery/. If I add [R] behind my RewriteRule it redirects correctly to …/theme/gallery/?title=Portfolio-Item but changes the URL, but I’m trying to use clean urls.

    Any idea?

    Thank you in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • And how about using WP rewrites instead of the Apache ones?

    add_rewrite_rule('^gallery/([^/]*)?','index.php?page_id=9&title=$matches[1]','top');

    Please note that after adding the above code to your functions.php file you need to refresh rewrite rules cache. To do this you either need to go to Dashboard -> Settings -> Permalink -> Save Changes, or add flush_rules() after the above code (not the best idea to keep it there as it’ll make WP regenerating rewrite rules upon every call which is a performance killer)

    Thread Starter beelde

    (@beelde)

    Hey thanks for your fast response. If I’m going to …/theme/gallery/Portfolio-Item he redirects me to …/theme/gallery/Portfolio-Item/ and loads the /gallery/ page and my contents like images will be fetched from /theme/gallery/ instead of /theme/.

    Thread Starter beelde

    (@beelde)

    The solution was quite simple:

    function rewrite_rules(){
        global $wp_rewrite;
        add_rewrite_tag('%title%','([^&]+)');
        add_rewrite_rule('^gallery/([^/]*)?','index.php?page_id=9&title=$matches[1]','top');
        $wp_rewrite->flush_rules();
    }
    add_action('init','rewrite_rules');

    Instead of using $_GET you have now to use $wp_query->query_vars[‘title’].

    I hope it will help you.

    Thread Starter beelde

    (@beelde)

    Don’t forget to comment $wp_rewrite->flush_rules(); after reload page first time.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘.htaccess RewriteRule’ is closed to new replies.