Created
February 28, 2012 09:19
-
-
Save caillou/1931490 to your computer and use it in GitHub Desktop.
Rewrite URLs in Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: ReWrite | |
Plugin URI: http://nelm.io/ | |
Description: A Plugin to ReWrite URLs. | |
Version: 0.1 | |
Author: Pierre Spring | |
Author URI: http://nelm.io/pierre | |
License: GPL2 | |
*/ | |
/* Copyright 2011 Pierre Spring (email : [email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
class ReWrite | |
{ | |
public static $lang; | |
public static $pageId; | |
public static function hookActivate() | |
{ | |
// flushes the modrewrite rules when | |
// activating the plugin | |
global $wp_rewrite; | |
$wp_rewrite->flush_rules(); | |
} | |
public static function hookDeactivate() | |
{ | |
} | |
public function modRewrite($rules) | |
{ | |
$home_root = parse_url(home_url()); | |
if ( isset( $home_root['path'] ) ) { | |
$home_root = trailingslashit($home_root['path']); | |
} else { | |
$home_root = '/'; | |
} | |
$ReWrite_rules = array( | |
'', | |
'# ReWrite Plugin: Begin Custom htaccess', | |
'<IfModule mod_rewrite.c>', | |
'RewriteEngine On', | |
'RewriteBase ' . $home_root, | |
'RewriteCond %{REQUEST_URI} ^/$', | |
'RewriteRule (.*) /de/$1 [R=301,L]', | |
'', | |
'RewriteCond %{REQUEST_URI} ^/(\w{2})/news/?$', | |
'RewriteRule .* /category/%1/ [L]', | |
'</IfModule>', | |
'# ReWrite Plugin: End Custom htaccess', | |
'', '' | |
); | |
#RewriteCond %{REQUEST_URI} ^/\w{2}/blog | |
#RewriteRule (.*) /category/de/ | |
#RewriteCond %{REQUEST_URI} ^/\w{2}/\d{4} | |
#RewriteRule /\w{2}(.*) /$1 | |
$ReWrite_rules = implode("\n", $ReWrite_rules); | |
return $ReWrite_rules . $rules; | |
} | |
public static function actionInit() | |
{ | |
// die(var_dump($_SERVER['REDIRECT_URL'])); | |
// parse url | |
$baseDir = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'); | |
$path_array = explode('/', trim(substr($_SERVER['REQUEST_URI'], strlen($baseDir)), '/')); | |
// extract language if present | |
if (in_array(reset($path_array), array('de', 'en'), true)) { | |
self::$lang = array_shift($path_array); | |
// TODO WP_LANG should be defined accordingly in wp-config.php | |
} | |
// leftovers = page id | |
self::$pageId = implode('/', $path_array); | |
// Wordpress only look out for REQUEST_URI, and never for REDIRECT_URL. | |
// As we make extensive usage of rewrite rules, this has to be fixed here. | |
// c.f. http://core.trac.wordpress.org/ticket/15478 | |
if (isset($_SERVER['REDIRECT_URL']) && $_SERVER['REDIRECT_URL']) { | |
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL']; | |
} | |
} | |
} | |
register_activation_hook(__FILE__, array('ReWrite', 'hookActivate')); | |
register_deactivation_hook(__FILE__, array('ReWrite', 'hookDeactivate')); | |
add_action('init', array('ReWrite', 'actionInit')); | |
add_filter('mod_rewrite_rules', array('ReWrite', 'modRewrite')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment