URL rewriting via Wordpress Rewrite or .htaccess

后端 未结 4 1416
离开以前
离开以前 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:20
    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.

    0 讨论(0)
  • 2020-12-03 02:29

    URL rewriting wouldn't work for me, neither by calling add_filter('rewrite_rules_array', function() {}), nor by calling add_rewrite_rule(). Apparently that was because filter 'rewrite_rules_array' was never triggered when opening frontend website pages, so the rewrites weren't generated.

    The solution was to go to admin area and just open page Settings -> Permalinks. I need to do that every time I want to change my rewrites.

    I don't know if vanilla Wordpress works like that, this is probably caused by an plugin/theme a have installed.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 02:38

    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)

    0 讨论(0)
提交回复
热议问题