No rewrites § www.nytimes.com/article.php?article_id=349590 Using rewrites § www.nytimes.com/2012/06/07/face-eater-feasts-in-miami WHAT ARE REWRITES?
processes URL through a rewrite module (mod_rewrite) Rewritten script executed Result returned to visitor www.nytimes.com/2012/06/07/ face-eater-feasts-in-miami index.php?pagename=face-eater-feasts-in-miami .htaccess file
mod_rewrite is available RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] # No substitution, load index.php RewriteCond %{REQUEST_FILENAME} !-f # Path is not an existing file RewriteCond %{REQUEST_FILENAME} !-d # Path is not an existing directory RewriteRule . index.php [L] </IfModule> # END WordPress WEB SERVER REWRITE RULES ¡ Generated by WordPress ¡ Sends any pretty permalinks to WP’s index.php for processingt *example using Apache mod_rewrite
(AND NOT SO COOL) STUFF WITH .HTACCESS RewriteCond %{HTTP_USER_AGENT} MSIE\s7{1} RewriteRule . http://www.google.com/chrome/ [L] ¡ Access to variables like HTTP Headers, server internals, date/ time, requester’s info ¡ Block external access to image files RewriteCond %{HTTP_REFERER} !^https?://www\.yoursite\.com/ RewriteRule \.(?:gif|jpg|jpeg|png)$ /blocked.png [L]
WordPress query arguments index.php?tag=$matches[1] Would get set to index.php?tag=music Regular Expression to match against URL tag/([^/]+)/?$ Matches the URL www.yoursite.com/tag/music 1 2
index.php?tag=$matches[1] search/(.+)/?$ index.php?s=$matches[1] feed/(feed|rdf|rss|rss2|atom)/?$ index.php?&feed=$matches[1] page/?([0-9]{1,})/?$ index.php?&paged=$matches[1] ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$ index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4] Single category archive Single category archive paginated Single tag archive Search Site feeds Post archive paginated Single day archive paginated regex rewrite
/store/ - archive of all products ¡ /store/american-pilsner/ - archive of products under a custom taxonomy ¡ /store/american-pilsner/case-of-yuengling/ - single product prepended a custom taxonomy term Gracefully degrading archives for a custom post type ¡ /events/%year%/%monthnum%/%day%/%postname%/
and process • Take first rewrite rule regex that matches URL Allowed query variables turned into WP query variables. • Populate $wp_query object with query variables. Template loaded • WP Query Variables • Template Hierarchy
$rewrite as $match => $query ) { // If the requesting file is the anchor of the match, prepend it to the path info. if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request ) $request_match = $req_uri . '/' . $request; if ( preg_match("#^$match#", $request_match, $matches) || preg_match("#^$match#", urldecode($request_match), $matches) ) { if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { // this is a verbose page match, lets check to be sure about it if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) ) continue; } // Got a match. $this->matched_rule = $match; break; } } wp-includes/class-wp.php:196-215 ¡ Process stored rewrites, if a rewrite rules is matched, set query variables.
taxonomy for recipe group. § /southern-cooking/roasted-okra/ (/custom-taxonomy-term/custom- post-name/) § Matching rewrite rule is too general – /(^/)+/(^/)+/ - would catch all posts § Is possible § Create a rewrite rule for each taxonomy term § /southern-cooking/(^/)+/ § /chinese/(^/)+/ § /amish-and-mennonite/(^/)+/ § Easier to add a specific string to the rewrite rule § /recipes/(^/)+/(^/)+/ § /recipes/southern-cooking/roasted-okra/ THE MORE SPECIFIC THE BETTER
add_rewrite_rule() ¡ A string of rewrite tags that generates multiple rewrite rules § Available in core: %year% %monthnum% %day% %hour% %minute% %second% %post_id% %postname% %category% %author% § Extensible with add_rewrite_tag() ¡ Can walk directories - creates piecemeal tag rewrites § /%year%/ - /2012/ - all posts from 2012 § /%year%/%monthnum%/ - /2012/06/ posts from June 2012 § /%year%/%monthnum%/%day%/ - /2012/06/09/ posts from June 9 2012 ¡ Adds RSS feeds for each level of archives PERMASTRUCTS
of last name § /authors/%author_firstname_firstletter%/ § /authors/A/ ¡ Date archives for CPTs § /event/%year%/%monthnum%/%day%/ § /event/2012/06/09/ ¡ Whatever you can dream.
of existing permastructs. § View post in JSON format § www.yoursite.com/a-post/json/ § View a gallery of attached images to a post § www.yoursite.com/a-post/gallery/ ¡ Opportunity to show a post’s data in a different format.
'catch_gallery_endpoint'); function catch_gallery_endpoint() { global $wp_query, $post; // if this is not a request for json or a singular object then bail if ( isset( $wp_query->query_vars['gallery'] ) && is_singular() ) { $post = get_queried_object(); locate_template('gallery.php', true, true); exit; } }
¡ Helper class to add custom taxonomy to Post Permalinks ViperBond007 § http://www.viper007bond.com/2011/10/07/code-snippet-helper- class-to-add-custom-taxonomy-to-post-permalinks/ ¡ Rewrite Endpoints API by Jon Cave § http://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints- api/ GOOD REFERENCES