-7

How do I convert

example.com/company?name=famoustech to static link example.com/company/famoustech from HTML tag

Example <a href="example.com/company?name=Famoustech">Famoustech</a>

Once user click the link how to convert it to

example.com/company/famoustech

Using .htaccess also to fetch the value from database in example.com/company page

How can I achieved this in PHP and MYSQLI This is what I have done so far On Index <a href="/company/<?= $row['company']?>"><?= $row['company']?></a> which look example.com/company/famoustech

On Company page $company_name = $_GET['company']; but is 500 internal server error

1 Answers1

0

I'm having to guess at your configuration a bit, but this probably isn't something you are going to do in PHP (unless you are already using a routing script, but I would guess you are not).

You are probably looking for mod_rewrite (see example 2), which is usually configured in an .htaccess file.

Here is an example .htaccess file

RewriteEngine On

RewriteRule ^company/(.*)$ ./company/index.php?name=$1

mod_rewrite uses regular expressions. This file says, when you get a request that matches the regular expression ^company/(.*)$, route the request to ./company/index.php?name=$1 (note: $1 will be replaced with the first capture group from the regular expression, $2 will be replaced with the second, etc).

9072997
  • 849
  • 7
  • 21
  • Ok but how will I get the value back in page using GET method – Famoustech Jun 09 '23 at 15:47
  • After seeing the edits to the question I think I might have misunderstood you. Is your problem that you have links like `example.com/company?name=Famoustech` that you want to look like `example.com/company/famoustech`? I don't understand how the database is related. – 9072997 Jun 09 '23 at 15:53
  • Yes and I want to use GET method to fetch it in company page in database – Famoustech Jun 09 '23 at 15:55
  • 1
    Just change it to `RewriteRule ^company/(.*)$ ./company/index.php?name=$1`. This should rewrite a request like `/company/foobar` internally as `/company/index.php?name=foobar`, making `$_GET['name']` accessible. – M. Eriksson Jun 09 '23 at 15:58
  • _Side note:_ Just make sure you handle a state when it's not set though, unless you have another rewrite for `/company`. If not, that path will also go to `/company/index.php`, but without any query parameter set. – M. Eriksson Jun 09 '23 at 16:01
  • M.Eriksson this my structure project ```-index``` ```-company``` ```-solution``` – Famoustech Jun 09 '23 at 16:08