-1

I am keen to understand how I might be able to do the following.

A list of products or things that might be a JSON data object or flat text file of things.

Let's say the list of things is:

twitter
facebook
instagram

I want to get a URL system that does this:

domain.com/advice-on-twitter
domain.com/advice-on-facebook
domain.com/advice-on-instagram

where twitter or facebook is the variable and I want domain.com/advice-on-<random string> to 404.

So I am seeing how to make a slug cause a template to load according to the template but I don't want to create a whole page in the backend with a slug.

So I want to make a dynamic page creation idea. The variable will be used with php to build content that is unique and render that per page.

I don't think there is much that can be done with .htaccess for this problem.

I want to get the string from the URL in PHP and therefore build my content.

I just need to get a page-template PHP file to load where I can start my PHP work.

Hope this makes sense.

Thank you for any pointers as it is a loaded question to a degree.

0

I would actually tackle it with the .htaccess file, using RewriteCond and RewriteRule, something like:

RewriteCond %{REQUEST_URI} ^/advice-on-(\[a-zA-z])$
RewriteRule ^/?(.*) http://domain.com/customadvice?advice=$1 [L]

Note that the Regex in the parenthesis will be reused as $1 in the RewriteRule.

You should then have a WordPress page on /customadvice and use the $_GET attribute from the page template for whatever use you need.

The path /advice-on should still give a 404.

  • Oh this could work, I will get testing thank you. My issue for not using htaccess was that it couldn't query my list of 'things' but I can do this check later and 404 if the url stripped doesn't match them...TY Marc-Antoine. – landed Jan 13 '17 at 11:37
  • I am struggling to get a match for RewriteCond %{REQUEST_URI} ^/advice-on-([a-zA-z])$ will continue to see why.... – landed Jan 13 '17 at 14:10
  • RewriteCond %{REQUEST_URI} ^/advice-on-[a-zA-Z]*$ [NC] – landed Jan 13 '17 at 17:02
  • Ok So I can now get the variable in my page but creating a new page has changed the url.. I need the url to be domain.com/advice-on-twitter vs domain/customadvice/advice-on-twitter and the issue is the redirect keeps recursing...because it matches always "advice-on-" – landed Jan 13 '17 at 17:35
  • That is why I used parenthesis :) It will return only your advice string (i.e. twitter) in the $1 variable, so it will not catch anymore in the RewriteCond. By the way, using the * symbol is gonna catch with ou without, so the URL /advice-on- would return true too. The right thing to use should be RewriteCond %{REQUEST_URI} ^/advice-on-(\[a-zA-z]+)$, the plus symbol meaning one or more. Sorry for the inconvenience, missed it at first :) – Marc-Antoine Parent Jan 14 '17 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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