URL rewriting via Wordpress Rewrite or .htaccess

2019-01-11 07:58发布

问题:

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

回答1:

There is another way to solve it, without touching WordPress.

Add to .htaccess

RewriteRule ^lion /indexw.php?pagename=db&dios=$1 [L,E=URI:/detail/]

Create file indexw.php:

<?php $_SERVER["REQUEST_URI"] = $_SERVER["REDIRECT_URI"]; include("index.php");

How it works? mod_rewrite sets REDIRECT_URI as specified in E=URI:value argument. indexw.php just overwrites REQUEST_URI with correct value (Thanks to Ravi Thapliyal for an insight on this)



回答2:

WordPress puts a global redirect on root for everything unidentified i.e. not an existing file or a folder to its /index.php. You on the other hand now want to redirect them to ?animal=unidentified. So, unless the list of animal keywords is fixed any solution proposed could mess up your WordPress.

If you had like 10-odd animals you could add them like below to your .htaccess (at root /)

RewriteEngine on
RewriteBase /

RewriteRule ^(lion|leopard|tiger|dog|cat)/?$ database/?animal=$1 [NC,L]

# WordPress rules come here

For 40-odd animals I would suggest you to have a directory (need not exist) prefix for your animals.

RewriteRule ^a/(.+?)/?$ database/?animal=$1 [NC,L]

This would redirect any /a/critter to database/?animal=critter and you won't have to add them to your .htaccess manually any more. You could also have both the rules co-exist so that if you haven't modified .htaccess for /panther yet you could still access it at /a/panther.

EDIT:
Okay, I looked into it and it isn't possible without writing a PHP script to intercept this request and forward it to index.php. Here's how mutisite works: since, none of your rewrites match it goes to index.php; the entry point for WordPress's php code. Somewhere deep, the code checks the REQUEST_URI header to see if it matches one of your multisites (/dios) and if it does, forwards the request to the page configured (dios.php).

When we do an .htaccess redirect for /dioses/agni we're able to hit index.php (by removing the [L]) but the REQUEST_URI header still remains the same (/dioses/agni) and it has no mulisite configured for it. Hence, the redirection fails.



回答3:

RewriteEngine On
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^www\.animals\.com/lion$ /www.animals.com/database/?animal=lion [R=301,NE,NC,L]

Just use this code. I hope that it will works.