0

I am setting a WP that it should redirect all the request made to sub-directory to file inside that directory. For example if there a visit on example.com/link/me then it should send the user to /link/index.php?me

To achieve this, I am trying to add rewrite rule in theme function file. Here's what my code looks like:

function moin_add_rewrite_rules() {  
    add_rewrite_rule(  
        '^([^/]*)/(link)/(?:[a-z][a-z0-9_]*)?$',  
        '/link/index.php?$matches[1]',  
        'top'  
    );  
}  
add_action( 'init', 'moin_add_rewrite_rules' );

It is not working, visiting example.com/link/meshows a 404 (as link is outside WP). Is there any problem with regex code? or anywhere else? What could be other possible solution?

4

1 回答 1

1

Looking at the example in the Codex, the matched expression doesn't have the leading /.

Stripping the leading bit out of your regex, then, I think this should work (sorry, I don't have a test environment handy):

add_rewrite_rule('^link/([a-z][a-z0-9_]*)/?$','/link/index.php?$matches[1]','top');

Alternatively, you could do something similar in your .htaccess file.

As a side note, I think as it's currently coded, you should be using $matches[3] not $matches[1], as the value you're interested in is the third parameter in parentheses (the Codex notes "capture group data starts at 1, not 0").

于 2013-03-30T22:24:47.307 回答