I'm building a dynamically populated profile page for my site users. This currently uses the following URL structure:
/?&uid=XX
I'd like to improve the look of the URL by inserting the user's nicename to create this URL structure:
/{nicename}/?uid=XX
I know I should be able to do this using a fairly simply rewrite rule in .htaccess
, but haven't been able to get the right rule setup.
How can I get this structure / rewrite setup?
I'm building a dynamically populated profile page for my site users. This currently uses the following URL structure:
https://example/members-list-profile/?&uid=XX
I'd like to improve the look of the URL by inserting the user's nicename to create this URL structure:
https://example/members-list-profile/{nicename}/?uid=XX
I know I should be able to do this using a fairly simply rewrite rule in .htaccess
, but haven't been able to get the right rule setup.
How can I get this structure / rewrite setup?
It's the redirect in htaccess that will route the "pretty" url into the real querystring based url that is the issue.
So, it would seem you need to "remove" the /{nicename}
portion from the "pretty" URL of the form https://example/members-list-profile/{nicename}/?uid=XX
.
Try something like the following at the top of your .htaccess
file (before the WordPress front-controller) using mod_rewrite:
RewriteRule ^(members-list-profile/)[^/]+/$ $1 [L]
However, unless this maps to a physical file (in which case you should rewrite directly to that file, rather than let mod_dir issue a subrequest) then this might not work with WordPress, since WP still routes the URL based on physical/visible URL - it doesn't necessarily see the rewritten URL. You could issue an external redirect to get around this, but that really defeats the point of having {nicename}
in the URL to begin with.
Something like this should probably be done entirely within WordPress, not .htaccess
.
{nicename}
part of the URL? – MrWhite Commented Dec 18, 2018 at 22:46