JUMP TO EDIT8 TO SEE HOW I SOLVED THIS
Let's say I have a Wordpress blog: www.animals.com
. I have a certain PHP file in my theme directory: www.animals.com/wp-content/themes/mytheme/db.php
. Also, I have a custom template for that page, so I create a new page in the Wordpress administration panel to show db.php
: www.animals.com/database
.
So if I want to read something about lions, I just go to: www.animals.com/database/?animal=lion
(because that's the way I decided to write the PHP, inserting the value from $_GET['animal'] into the query, using PDO etc.).
Now, I would like to access www.animals.com/database/?animal=lion
as www.animals.com/lion
.
Should I use .htaccess or Wordpress Rewrite? Where should I place my .htaccess, in the root of the Wordpress folder (with wp-config.php and those files) or in my theme directory?
The one from the root has RewriteBase /
and stuff from Wordpress by default. What should I write to achieve what I want? Should I put it before or after the existing code?
EDIT: this is my public_html .htaccess and this is what I really want to do:
I have a website: www.domain.com
and when you type this http://www.domain.com/dios/?dios=agni
it shows you the info about the god Agni. I would like to type www.domain.com/dioses/agni
to access the info about the god Agni. The PHP file is in www.domain.com/wp-content/themes/smitespain/dios.php
It's not working x_x
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dioses/(\w*)$ dios/?dios=$1 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT2: i'm using multisite (check EDIT4), and again, /database/ /dios/ and /dioses/ are not actual folders I created, thats the name for the page I created in Wordpress. The thing is, now it shows the name of the god in the tab title :S that means the variable $_GET['dios'] is set, but it doesnt load the file (dios.php) =/
EDIT3: I need to access domain.com/dios/?dios=agni specifically, because thats the URL that will let the PHP file (dios.php) to load get_header() from wordpress, so i cant access the file directly
EDIT4: I decided to remove the multisite thing
EDIT5: these are the Wordpress pages I have:
domain.com/dioses/
for (www.domain.com/wp-content/themes/smitespain/dioses.php)
domain.com/dios/?dios=thor
for (www.domain.com/wp-content/themes/smitespain/dios.php)
EDIT6: i was reading something in the wordpress codex..and realized that, if i wanna go to domain.com/dioses
i can access it by going to domain.com/index.php?pagename=dioses
or domain.com/?pagename=dioses
So, i added this between the Rewritebase /
and the next rule: RewriteRule example/test ?pagename=dioses
and domain.com/example/test
redirects me to domain.com/dioses
but it also changes the url in the address bar :(
The thing is, if i try this: RewriteRule example/test ?pagename=dios&dios=thor
it will send me to the page 'dios' without the '?dios=thor'
EDIT7: i started using wordpress rewrite, i added this to my theme's functions.php:
function my_rewrite_rules() {
add_rewrite_rule(
'blah',
'index.php?pagename=dios&dios=agni',
'top'
);
}
add_action( 'init', 'my_rewrite_rules' );
and again..it loads the page dios, without the ?dios=agni
EDIT8: and finally, I managed to make it work =)
the first thing i needed to know is, the new ?dios=XXXX
will be no longer available to $_GET['dios']
instead, you need to call $wp_query->query_vars['dios']
so i added this to my theme's functions.php
function add_query_vars($new_var) {
$new_var[] = "dios";
return $new_var;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($rules) {
$new_rules = array('([^/]+)' => 'index.php?pagename=dios&dios=$matches[1]');
$rules = $new_rules + $rules;
return $rules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
i make sure $wp_query->query_vars['dios']
is actually set
then i just add the regular rule