3

I'm not able to make it work by adding custom rewrite rules into functions.php or adding custom permastructures either. I have this code in my .htaccess that is working fine.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# BEGIN IPHONE RULES
RewriteRule ^mobile/([^/]+)$ mobile/index.php?action=$1 [QSA,L]
# END IPHONE RULES
# BEGIN PDF RULES
RewriteRule ^certificates/30-Days-Certificate-([0-9]+)$ pdf/index.php?type=30day&period=$1 [QSA,L]
RewriteRule ^certificates/12-Month-Certificate$ pdf/index.php?type=12Month [QSA,L]
# END PDF RULES
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

But every once a while it gets refreshed to defaults and I lose the iPhone and pdf rules. The mobile is called like this /mobile/progress?userId=22&date=2011-02-21 where progress is the action and the others are just query vars. On the .htaccess is working fine but I can't manage to make it work by adding custom rules. Any help will be much appreciated.

1
  • What did you try? Paste your code
    – Bainternet
    Mar 22, 2011 at 11:58

1 Answer 1

2

The part between the # BEGIN WordPress and # END WordPress will always be rewritten when the permalinks are flushed. You can either place your extra rewrite rules before this segment, or you can add register them in WordPress as external rewrite rules. If you flush your rules now (by visiting the Permalinks page, for example), your extra rules will be added to the .htaccess file.

add_action( 'init', 'wpse12708_init' );
function wpse12708_init()
{
    global $wp_rewrite;
    $wp_rewrite->add_external_rule( 'mobile/([^/]+)$', 'mobile/index.php?action=$1' );
    $wp_rewrite->add_external_rule( 'certificates/30-Days-Certificate-([0-9]+)$', 'pdf/index.php?type=30day&period=$1' );
    $wp_rewrite->add_external_rule( 'certificates/12-Month-Certificate$', 'pdf/index.php?type=12Month' );
}
7
  • Wow Thanks, its almost working now. Before it was not adding a rule at all. <br/> Now Checking the htaccess file its adding the rules but its also adding a / into them. <br/> So instead of RewriteRule ^mobile/([^/]+)$ mobile/index.php?action=$1 [QSA,L] i have in the htaccess RewriteRule ^mobile/([^/]+)$ /mobile/index.php?action=$1 [QSA,L] . So im getting a 500 internal server error.
    – chifliiiii
    Mar 22, 2011 at 12:32
  • For what i saw if i edit the wp-includes/rewrite.php i can erase the $home_root var in order to remove the /, but it would be great to have a proper solution or at least know why its adding the /. Thanks again for all your helo
    – chifliiiii
    Mar 22, 2011 at 12:41
  • @chifliiiii: Ah indeed, WordPress prepends the $home_root for you. But this should not lead to an invalid .htaccess file. Do you also get the error when you go to /mobile/index.php?action=[something] directly?
    – Jan Fabry
    Mar 22, 2011 at 12:55
  • no I Dont get any error by going direclty. IT could be that maybe its trying to go to www.someweb.com//mobile ? Becasue as soon as i remove the / its working fine
    – chifliiiii
    Mar 22, 2011 at 13:47
  • @chifliiiii: Do you have a directory /mobile with an index.php file at the root of your server filesystem? Because Apache will look there too if the replacement starts with a /. Adding / is normal, WordPress does it too in the /index.php redirection.
    – Jan Fabry
    Mar 22, 2011 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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