Search code examples
phphtmlcsspermalinks

How to change existing permalink (PHP, HTML, CSS)


I have a problem with permalink (I think those are called permalink). I am developing WEB site using PHP, HTML and CSS. The main problem is when I send data to another page, example is:

<a href="project.php?id=1">Project</a>

I get that id in project.php but in URL there is mysite/project.php?id=1, instead of that, how can I make it look like this mysite/project, I want to get rid of .php?id=1, but I want to keep id on project page. I wrote this post a bit confusing but if you need some more info just say. Thanks in advance !!!


Solution

  • The proper name for what you are describing is "friendly URL" or "clean URL". Yes, that often relates with the notion of "permalinks", which describes the fact that a given URL is intended to remain permanently valid; however "permalinks" do not actually forbid having query args or keeping file extensions.

    How it works

    Now, implementation of friendly URL in PHP always starts by adding some form of location handler at the web server's level. Essentially, you need the web server to understand that a request of the form /mysite/project/<id> actually need to be delegated to some specific PHP script (more on this later).

    Then, you also need some form of routing mechanism that will decode the original URL, decide what action is being requested and what arguments are to be provided; for example, that routing mechanism would decode the URL /mysite/project/2 and determine that it has to return the project page template, filled with project id 2.

    In complex PHP applications, these two concerns are usually dealt separately. In such case, a single rule will be added in the web server's configuration file, which redirect every request (with the exception of requests on static assets) to a unique PHP entry point, traditionally named "index.php". Then, some algorithm in that file will inspect the $_SERVER['REQUEST_URI'] variable, decode it, and determine what PHP function to invoke, and with which arguments. Such an algorithm is typically named a "router". This is for example how applications such as WordPress, Magento or Prestashop works.

    Alternatively, it is also possible to handle both concerns, at the web server's level, and doing so is generally easier on very small projects (note that there's pros and cons to that approach, which I won't discuss in details at this point). In that case, you would add to your web server's configuration one rewrite rule per URL pattern, substituting the friendly URL by its internal equivalent. For example, /mysite/project/<id> could be internally rewritten to /mysite/project.php?id=<id>.

    How to do it

    Developing a PHP based router from scratch is not really that hard, but I would not recommend you to do so. If you think your project's might grow to the point where having a PHP based router might be pertinent, then it might also be pertinent to consider using a development framework that already support routing. Most every PHP frameworks do include some form of routing. I personally won't recommend any, just search for "PHP frameworks" in Google.

    As for implementing friendly URLs by the mean of rewrite rules at the web server's level, the syntax obviously depend on which web server you are using. Most servers support some form of regex based syntax for that purpose.

    For example, using nginx (see nginx rewrite documentation), you could have something like this:

    server {
        # ...
    
        rewrite "^/mysite/project/([0-9]+)$" /mysite/project.php?id=$1;
    
        # ...
    }
    

    Using Apache httpd (see Apache's mod_rewrite documentation), you could obtain the equivalent by adding this to the .htaccess file, inside the root directory of your site:

    RewriteEngine On
    RewriteRule "^/mysite/project/([0-9]+)$" "/mysite/project.php?id=$1" [L]