Handling Dynamic URL with WordPress

16 views
Skip to first unread message

petervan...@gmail.com

unread,
Jul 15, 2021, 9:27:10 AM7/15/21
to Minneapolis St. Paul WordPress User Group
Hi all,

I am trying to figure out the best way to handle URLs of the form https://mysite.com/is/name.  The "name" part of the URL is variable.  Essentially, I'm going to be returning a PDF based on that dynamic information.

I'm not sure what the best way to do that within WP is.  It would be easier if the URL was just a querystring like https://mysite.com/is?name=xxxx, but these are preexisting links.  Maybe I should figure out how to redirect?

- Peter

barbara schendel

unread,
Jul 15, 2021, 9:47:12 AM7/15/21
to mpls-stpau...@googlegroups.com
Would something like the "URL Params" plugin work? https://wordpress.org/plugins/url-params/

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpls-stpaul-wordpress/7201e81d-5928-479d-bbfc-72978d0b95b4n%40googlegroups.com.

Peter VanKoughnett

unread,
Jul 15, 2021, 10:02:35 AM7/15/21
to mpls-stpau...@googlegroups.com
Thanks, Barbara. I think that once I get to that point, I could just parse the query strings myself, but the current URL doesn't use query strings.

Eric Celeste

unread,
Jul 15, 2021, 10:20:41 AM7/15/21
to mpls-stpau...@googlegroups.com
Peter, this does seem like a job for Apache or Nginx URL rewrites. Do you have access to your server at a level that allows rewriting the URLs?

If you do, you could rewrite the appropriate URLs to move the “dynamic” part of the URL into a parameter and deal with it more easily.

Another alternative, if you don’t have access to the rewrites of your server, is to handle the problem in the 404.php script of your theme. When WordPress fails to find the correct page, it will call the 404 script. In that script you can include code to look for your special case and handle it.

...Eric

Eric Celeste / e...@tenseg.net / 651-323-2009


Nick Ciske

unread,
Jul 15, 2021, 12:33:15 PM7/15/21
to mpls-stpau...@googlegroups.com
There’s a bunch of ways to accomplish this, some already mentioned, but here’s a few more:
  1. Use a redirect plugin like Safe Redirect Manager that supports capturing parts of the URL and redirect to a page with a URL parameter vs the pretty permalink e.g. /is/cool becomes /is?n=cool
    1. Or, use a Cloudflare page rule and save an extra hit to your server
    2. Or, use a custom Niginx or Apache rewrite to do this behind the scenes (this is what WP does for pretty permalinks)
  2. Use add_rewrite_rule to add a custom rule to the WP permalink engine… that you can than catch in a hook to handle the output of your new URL, this is 1b but using the existing rewrite engine in WP
    1. https://www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/

_________________________
Nick Ciske
CTO/CISO | LuminFire

Patrick Lewis

unread,
Jul 15, 2021, 1:33:45 PM7/15/21
to Minneapolis St. Paul WordPress User Group
I think a WordPress rewrite rule would work well for this case. Here is the developer documentation for add_rewrite_rule(). 


The top-ranked contribution has a simple example that matches closely the URL pattern you hope to use.

If you go this route, be sure to flush your rewrite rules before testing your new URL. This can be done programmatically or by going to the dashboard Settings -> Permalinks and click "Save." No need to actually change anything. Saving will flush the cache.

Peter VanKoughnett

unread,
Jul 15, 2021, 3:56:46 PM7/15/21
to mpls-stpau...@googlegroups.com
Thanks all! I'll probably go the .htaccess route.

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages