Title: Cannot rewrite rule
Last modified: May 28, 2018

---

# Cannot rewrite rule

 *  [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/)
 * Hi I’ve searched and tried all the solutions but nothing works at the moment.
   I am developing the site in a sub folder and wonder if that has anything to do
   with it.
 * I have a page set up at [http://www.mywebsite.com/admin/where-to-eat-drink](http://www.mywebsite.com/admin/where-to-eat-drink)
   and I am trying to rewrite a rule so that various links that look like this
 *     ```
       www.mywebsite.com/admin/where-to-eat-drink/raj_pavilion
       ```
   
 * go to that page and are broken down so the last part of the url can be used to
   return the correct data :
 * `www.mywebsite.com/admin/where-to-eat-drink/&?var=raj_pavilion`
 * In my .htaccess file I have tried this
 *     ```
       # BEGIN WordPress
       <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /admin/
       RewriteRule ^index\.php$ - [L]
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /admin/index.php [L]
   
       RewriteRule ^where-to-eat-drink/(.*)$ where-to-eat-drink/?var=$1 [NC,QSA]
   
       </IfModule>
   
       # END WordPress
       ```
   
 * I’ve also tried adding a function in functions.php:
 * `add_rewrite_rule('^where-to-eat-drink/([^/]+)/?','where-to-eat-drink?var=$matches[
   1]','top');`
 * All I get is a 404 error
 * any ideas would be much apreciated.
 * Thanks

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10332340)
 * Which .htaccess file ? The one in the subdirectory or the website root ?
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10332354)
 * In the Admin folder. Once the site is ready to go live I will move the site out
   of the admin folder.
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10332357)
 * The website root also has a wordpress install which will be removed once we are
   ready to go live.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10332948)
 * Your .htaccess rule is ignored as written. Generally, the standard WP rewrite
   rules should be last in .htaccess. You shouldn’t modify anything within the WP
   comment delimiters because WP could overwrite this section.
 * I’d suggest you use RedirectMatch instead of RewriteRule, assuming your server
   has mod-alias installed. Most do.
 * If you use WP rewrites, I believe the destination has to be a file, usually index.
   php. Let’s say the page ID of where-to-eat-drink is 123. The destination could
   be `'index.php?page_id=123&var=$matches[1]'`
 * Any time you change WP rewrites, you must visit the permalinks settings page 
   or call flush_rewrite_rules(). You should not call the flush function in regular
   code that executes on each request. It should be added to rare or one time code
   like a plugin activation hook. Thus when developing your code, visiting the permalinks
   screen is easier.
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10343210)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) thanks very much for
   the heads up on WP comment delimiters and flushing permalinks settings. I can
   now redirect to the correct URL using your code and hitting the permalinks settings
   page to flush, however $_GET[‘var’] = nothing. Don’t suppose you know why that
   could be happening?
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10343223)
 * P.S. I tried both .htaccess and the function in functions.php. .htaccess still
   gave 404 but the functions.php redirected correctly but didn’t give me the variable
   attempted to access via $_GET[‘var’].
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10343257)
 * `www.mywebsite.com/admin/where-to-eat-drink/raj_pavilion`
 * now correctly redirects to
 * `www.mywebsite.com/admin/where-to-eat-drink/`
 * using the function below
 * full wp function:
 *     ```
       function custom_rewrite_rule() {
       	add_rewrite_rule('^where-to-eat-drink/([^/]+)/?','index.php?page_id=54&var=$matches[1]','top');
       }
       add_action('init', 'custom_rewrite_rule', 10, 0);
       ```
   
 * Call to provide me with ‘raj_pavilion’ to query database is
 * `$_GET['var']`
 * however this returns nothing.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10346700)
 * Hmmm. Try adding ‘var’ to the whitelist of public query vars by using the “query_vars”
   filter. Simply add the ‘var’ element to the passed array of current query vars.
   Then the value should be available as a query var and there would be no need 
   to get it from $_GET. The value should then be part of the WP_Query object’s 
   query_vars data.
 * Some redirects through .htaccess will strip query strings from the URL. Be sure
   the rewritten URL with query string (index.php?page_id=54&var=raj_pavilion) is
   not being redirected further. Check your access logs to see possible additional
   redirects.
 * If neither of those help, we will need to dig deeper. How and where are you trying
   to use $_GET[‘var’]?
 *  Thread Starter [cannon303](https://wordpress.org/support/users/cannon303/)
 * (@cannon303)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10355840)
 * Brilliant, never heard of the query_vars before. This worked thank you. I changed
   my ‘var’ to ‘place’ for no reason other than for better reference.
 * In functions.php :
 *     ```
       function filter__query_vars( $query_vars ) {
       	$query_vars[] = 'place';
       	return $query_vars;
       }
       add_filter( 'query_vars', 'filter__query_vars' );
       ```
   
 * Then in the document I could reference it by
 * `echo $place`
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10357425)
 * Awesome! I can’t explain how query_var works but $_GET doesn’t. My suggestion
   was a shot in the dark. I’m pleased it actually worked.
 * Changing variable names for better reference is an excellent reason! Otherwise
   you may revisit this code a year later and think “what the heck is this “var”
   for?”

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Cannot rewrite rule’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years ago](https://wordpress.org/support/topic/cannot-rewrite-rule/#post-10357425)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
