0

I'm of the understanding that Wordpress' .htaccess, like below:

# 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

sends all queries through to the index.php (unless the filename exists on the server etc).

So, if I go to http://example.com/checkout Wordpress rewrites that as http://example.com/index.php?pagename=checkout.

But what I don't understand is that I have, at the top of a page template

<?php print_r($_GET) ?>

and when I navigate to http://example.com/checkout that returns Array(). I'd expect something like Array('pagename' => 'checkout');

How can I access the variables that Wordpress uses when handling a URL?

3 Answers 3

2

It's not really rewriting the URL in the conventional sense, URL parsing happens entirely within PHP.

You can access the query vars in the global $wp_query->query_vars, or with the get_query_var function.

Also note that parse_request is the earliest action where this info will be available.

1
1

It calls index.php, not index.php?page=...

I'm not sure how WordPress does its routing, but you could use $_SERVER['PATH_INFO'] or $_SERVER['REQUEST_URI']

1

Don't get fooled by the names. mod_rewrite Rewrite Rules and WordPress Rewrite Rules are two very different things.

In the .htaccess, there are mod_rewrite rules that don't do anything other than run WP's index.php for any requests that don't directly hit an existing file or directory.

To figure out, what /checkout means, WordPress then relies on its' own set of rewrite rules which have nothing at all to do with mod_rewrite, so don't get confused.

WordPress' $redirect part looks like it passes variables via HTTP GET, but that isn't really the case. Those will be used as rewrite tags. See the documentation for add_rewrite_rule for a few examples and further explanation.

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.