Title: .htaccess RewriteRule
Last modified: August 21, 2016

---

# .htaccess RewriteRule

 *  [beelde](https://wordpress.org/support/users/beelde/)
 * (@beelde)
 * [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/)
 * 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)

 *  [x500.net](https://wordpress.org/support/users/x500/)
 * (@x500)
 * [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/#post-3678634)
 * 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](https://wordpress.org/support/users/beelde/)
 * (@beelde)
 * [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/#post-3678640)
 * 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](https://wordpress.org/support/users/beelde/)
 * (@beelde)
 * [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/#post-3678708)
 * 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](https://wordpress.org/support/users/beelde/)
 * (@beelde)
 * [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/#post-3678709)
 * 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.

## Tags

 * [htaccess](https://wordpress.org/support/topic-tag/htaccess/)
 * [permalinks](https://wordpress.org/support/topic-tag/permalinks/)
 * [rewriterule](https://wordpress.org/support/topic-tag/rewriterule/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [beelde](https://wordpress.org/support/users/beelde/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/htaccess-rewriterule-2/#post-3678709)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
