Previously, I’ve been creating my sitemap page with example.com?action=sitemap
That allowed me to easily test on my index page for a sitemap request with…
$_REQUEST['action']
However, I’d like to instead create the link to the sitemap with example.com/sitemap
And I’d like to know how I can parse the request for the appearance of “/sitemap
“
You can create a new rewrite rule in WordPress something like this:
You should only need to run these functions once (when your theme is installed for example) because the rewrite rules are stored in the database.
However, you’ll probably find that using this method you cannot access your
action
variable using$_REQUEST['action']
. To access your variable you’ll have add it to WordPress’query_vars
array, something like this:You can then retrieve the action variable using
get_query_var('action')
.should do the trick
I don’t really know WordPress so my answer will be framework/app agnostic, but you can use mod_rewrite to easily achieve this behavior (although that means either it is already enabled on your host, either you have config-edit access). Just put this in a
.htaccess
file at your web root:If you already have a
.htaccess
file with aRewriteEngine On
statement, you can safely include only the second line anywhere after your existingRewriteEngine
declaration.What it does is basically tell apache to treat all request on
/sitemap
as requests on/index.php?action=sitemap
.You can try checking the contents of the PHPs
$_SERVER["QUERY_STRING"]
variable. It should have your/sitemap
part with a possibility of having some additional parameters as well.Why not: $_SERVER or $_REQUEST