URL rewriting via Wordpress Rewrite or .htaccess

后端 未结 4 1415
离开以前
离开以前 2020-12-03 02:10

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 t

4条回答
  •  庸人自扰
    2020-12-03 02:32

    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.

提交回复
热议问题