How to have the URL not display the page name and extension

I am a very new / inexperienced web programmer and I am trying to design a site using PHP and MySQL.

I have noticed many dynamically created pages do not display their page name and extension.

Here is an example:
https://icv2.com/articles/news/view/43437/wizkids-visits-the-goblin-village

Currently I have 200+ pages of “sets”, which all have their own url’s. I am creating a site that is dynamically generated so that only 1 page is needed to create the “set” display, but when I post links on other social media sites, I want to be able to post a link that contains the name of the set as shown in the above example.

In my current new site design, to get to the set named “Goblin Village” I have assigned that set a number in my database, and I reference it this way: http://www.mysite.com/?id=23?type=set

Here is some of my guesswork of what I need to do, to make the above happen:

  1. Instead of assigning a number as a primary identifier, I need to assign a unique name (ie: “wizkids-goblin-village” instead of “23” )
  2. Instead of passing my query info on the URL, I need to store it as session variables.

This is as far as I have gotten.

Could someone please advise what I need to do to make this happen.

Thank you.

I posted the answer in the “other” forum you posted this question to.

Assuming an Apache server, this is more about htaccess than PHP. That’s where you will set up redirects to show different URLs.

An easy option I have done in the past is to include the ID number and name in the URL, so something like: 23-goblin-village would be the page you see, but htaccess is reading ?id=23.
Another way I have done it without an ID number is to have a unique handle in the database, so when you have: /goblin-village the server sees ?page=goblin-village, PHP $_GETs the page hadle and queries the database for it.

No, for the above to work I use htaccess.

RewriteEngine On

RewriteRule ^([0-9a-z-]+)$ /?page=$1 [NC,L]

That captures the string in the URL and places it as the page variable as $1.

Yes, I think this is along the lines of what I am thinking.

Sorry, I am not completely up to speed on coding lingo. The reference to “page handle” – do you mean the part of the URL that is going to be my unique identifier in my database. In the above example, “wizkids-goblin-village” will be my page handler?

So then using the htaccess coding you provided, “wizkids-goblin-village” will be stored as variable $1, and I will then I will be able to retrieve the matching info in my database via PHP for the unique identifier “wizkids-goblin-village”?

I am not at all familiar with editing my htaccess file. Do I basically just copy and paste the info below… is there anything else I should be aware of when I do this?

I have a joke site that parses the URL and does numerous tests to try and find

  1. If the first characters are numeric then look for an exact table ID record and return the record

  2. If a string then look for an exact match for the table TITLE and return the record

  3. If a string and not an exact match then look for a group eg PICTURES and paginate all records with pictures

  4. If none of the above then split the string into words and search the TITLE and BODY fields for like words and build an array (of TITLES) for pagination, unless the array only has a single value then return the record.

  5. If the above all fail the return a dynamic search routine that returns all TITLES and BODY that match the search criteria. Using mysql this routine is very fast, same as a Google search only searches my database. Search this forum for “Ajax dynamic search” for further details.

Edit:
I forgot to mention…

  1. If the requested URL does not match the displayed record then issue a 301 http_request and the correct meta tag. This helps SEO tremendously.

Edit:
I use a PHP Framework and unless the URL is an exact match then all requests are redirected to index.php which makes it easier to parse the URL.

I have thought this through, and I’m not certain that this will achieve what I am trying to do.

If I understand this correctly, this will change any URL from www.mysite.com/?page=section-name to www.mysite.com/section-name

However if I want to post the link on social media accounts as www.mysite.com/section-name, then will it also do the opposite and add in the “?page=” reference?

I find that a .htaccess file is too rigid and prefer a simple redirection.

Take a look at all the popular PHP Frameworks also WordPress and if the URL is not a path or an exact file name then the .htaccess fallthrough is index.php?parameters.

Try passing numerous parameters with index.php then use the following to show the server parameters:

<?php
declare(strict_types=1);
echo '<pre>'; // adds linefeeds
  print_r($_SERVER);
echo '</pre>';
die; // similar to exit;

Study my previous post for an outline of the steps to use the parameters.

Edit:
Try searching for pretty URLs

For removing file extensions,

By “handle” I mean in the context as a short name or alias for the page, something that will be used in the URL to identify the page, there will be a corresponding entry in the database to select with.

The first part of the rewrite rule is the regex that the server looks for in the URL: ^([0-9a-z-]+)$
That basically looks for a string made from any number of numbers, lowercase letters or hyphens, any other characters won’t match. If you need to match other characters you need to alter the regex accordingly.
The part within the brackets is what gets captured and stored in $1.
The second part is what the server forwards the page to, though you will still see the original URL in the browser address bar. The variable can be whatever you like, but I used page here.

As explained, it’s pretty much the opposite of that.
You see the shortened URL, but the server sees it with the variables and can use $_GET to get the handle.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.