1

I am migrating from an ancient static web site to Wordpress.

In the old site, I used htaccess and RewriteRule to simplify URLs and mask the underlying parms. One case was:

RewriteRule ^definition/([^/]+) /display-definition?word=$1 [NC]

This allowed people to specify a URL of:

domain.com/definition/love

And under the sheets executed instead:

domain.com/display-definition?word=love

I have created and tested the new Wordpress page, and manually passing it the parameter works fine. The htaccess redirect does not, and instead tosses visitors to the default Wordpress 404 page.

I have tried placing the RewriteRule in various places in htaccess, but the results do not differ.

Current attempted htaccess file is:

RewriteEngine on
RewriteRule ^definition/([^/]+) /display-definition?word=$1 [NC]
RewriteRule ^murphyism/([^/]+) /display-murphy?id=$1 [NC]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
1

The standard WordPress .htaccess file will do this for you if you select one of the "pretty permalinks" in the Settings / Permalinks page, like the "posts" option.

    # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I can't seem to get that to format properly, so check the source page here - https://codex.wordpress.org/htaccess

For more info, see the Codex page - https://codex.wordpress.org/Using_Permalinks#mod_rewrite:_.22Pretty_Permalinks.22

  • Thanks, but I am not seeing the connections here. First, I am using the de facto htaccess generated by Wordpress, and attempting to add the line in question So far, we are aligned. The original static site is at cynical.ws. As you can see there are other aspects aside from word definitions, so it is unclear how using the pretty permalinks function would correctly address the goals. I may be missing the obvious though. – user3277069 Jan 7 '17 at 23:06
1

You shouldn't do this in htaccess with wordpress. You should instead add a rewrite rule using add_rewrite_rule and then register the query var.

You would be looking at adding to rewrite rules to handle your two rewrites:

RewriteRule ^definition/([^/]+) /display-definition?word=$1 [NC]
RewriteRule ^murphyism/([^/]+) /display-murphy?id=$1 [NC]

For Definition:

add_action( 'init', 'wpse251676_definition_rewrite_rule' );
function wpse251676_definition_rewrite_rule(){
    add_rewrite_rule(
        'definition/([a-z0-9A-Z_-]+)/?$',
        'index.php?pagename=display-definition&definition_word=$matches[1]',
        'top'
    );
}

For Murphyism:

add_action( 'init', 'wpse251676_murphyism_rewrite_rule' );
function wpse251676_murphyism_rewrite_rule(){
    add_rewrite_rule(
        'murphyism/([a-z0-9A-Z_-]+)/?$',
        'index.php?pagename=display-murphy&murphy_id=$matches[1]',
        'top'
    );
}

You can then register the query vars with:

add_filter( 'query_vars', 'wpse251676_query_vars' );
function wpse251676_query_vars( $query_vars ) {
    $query_vars[] = 'definition_word';
    $query_vars[] = 'murphy_id';
    return $query_vars;
}

With this you can then use the builtin get_query_var in your template files or wherever necessary to get the variables instead of using $_GET['variable']

$word = get_query_var('definition_word'); //$_GET['definition_word']
$id   = get_query_var('murphy_id');       //$_GET['murphy_id']

NOTE: It's always good practice to prefix your variables so they have a unique name different from any formally defined or futureones

References:

  • Thanks. A couple of follow-ups if I may: 1) Where does this code get added? In other threads it looks as if it is added to theme files. Since I am using a commercial theme, which gets updated regularly, it seems bad practice to edit those files and thus be forced to re-edit them constantly. 2) This approach requires finding and revising every instance of $_GET, which is a painful new labor. to the project. It there simply no way of getting htaccess to do what is needed as opposed to this approach? – user3277069 Jan 8 '17 at 15:32
  • Not necessarily, you can add this to the functions file of a child theme, define your custom templates in the child theme and also add your query vars to it. That way, an update to the parent theme doesn't affect anything you've done – Tunji Jan 15 '17 at 6:07
  • I finally found time to look into your approach (thanks again for the guidance). I have run into a problem with the query function (the redirects appear to be working). – user3277069 Jan 29 '17 at 0:20
  • What problem are you having with the query function? – Tunji Jan 29 '17 at 1:33
  • I found the problem. The original add_filter code has a different function name in the 2nd parm than the function itself (wpse26388_ and wpse251676_). – user3277069 Jan 30 '17 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.