I have a page set up at https://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
]]>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.
]]>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.
]]>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’]?
]]>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