1

I just started learning php and i created a simple php website which contains posts, users. and i made posts links looks like : http://localhost/?post=3 my question is there any way to make the url looks differents like http://localhost/post/252155 without creating php pages for it.

Here is my code snippet

$results = mysqli_query($conn, "SELECT * FROM projects");
$project = array();
if (mysqli_num_rows($results) > 0) {
    while($row = mysqli_fetch_assoc($results)){
        $project[] = $row;
    }
}

$projects = 0;
$maxprojects = 14;
while ($projects < $maxprojects) {
    $projectId = $project[$projects]["project_id"];
    $projectURL = "../basic/?project=".$projectId ;
    $projectTitle = $project[$projects]["project_title"];
    $projectThumb = $project[$projects]["project_thumbnail"];
    $projectPrice = $project[$projects]["project_price"];
    echo "
        <li class='gig'>
            <div class='gig-content'>
                <span class='thumbnail'>
                    <a href='$projectURL'><img src='".$projectThumb."'></a>
                </span><!-- Gig Thumbnail -->
                <span class='title'>
                    <h2><a href='$projectURL'>".$projectTitle."</a></h2>
                </span><!-- Gig Title -->
                <div class='meta'>
                    <span class='price'>
                        <h1>".$projectPrice."$</h1>
                        <h4>Startin at</h4>
                    </span><!-- price -->
                </div>
            </div>
        </li><!-- Gig -->
    ";
    ++$projects;
}

thanks in advance!

saddam
  • 771
  • 7
  • 23

2 Answers2

2

To accomplish this, you need to first create a text document called ".htaccess" to contain your rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). There may already be a .htaccess file there, in which case you should edit that rather than overwriting it.

RewriteEngine On 
RewriteRule    ^post/([0-9]+)/?$    ?post=$1    [NC,L]

NOTE: Only If Application runs on Apache

Kingsley Solomon
  • 433
  • 1
  • 4
  • 9
  • thanks! it worked! what if i want to add the post_name instead of it's id number what should i do? – Saīd Benmūmen Feb 07 '18 at 12:41
  • you can go ahead and use this same approach for post_name, but you have to make sure your PHP code is expecting a post_name and not a post_id. Let's say initially you are making a query to your database to get a post with a specified id in the URL, then you'll have to edit the query to get a post with the specified post_name in the URL and not post_id. – Kingsley Solomon Feb 07 '18 at 23:10
0

A common approach is for you to have the web server route all URL requests to any arbitrary (so long as an actual resource–aka file–does not exist at that location). With the Apache web server, you can use create a .htaccess to route all URLs for which no file or directory exists, to your index.php file. For example, at the root of your publicly facing directory, its .htaccess could contain

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

This will route all URLs that do not exist as a file or directory to index.php in the root directory for the website. (This is the way that WordPress does it).

Then, within your index.php, you can parse the URL and serve up whatever content you wish base on the URL (or not). You can create your own routing system, within your PHP code, to direct content generation to whichever PHP page you prefer.

<?php
print_r($_SERVER['REQUEST_URI']);

For example, https://yourdomain.com/post/13452345 would output, /post/13452345.

%_SERVER['QUERY_STRING'], parse_url() and might also be useful. And installation/configuration dependent values might also be available: $_SERVER['SCRIPT_URI'] and $_SERVER['SCRIPT_URL']

More information can be found at the PHP reference site Predefined Variables, $_SERVER

You can parse the URI as you wish:

$URI_COMPONENTS = explode( '/', $_SERVER['REQUEST_URI'] );
if ($(URI_COMPONENTS[0] == 'post') {
    // get $(URI_COMPONENTS[1])
    // output response based on post value
    // Maybe something like put_post_content($(URI_COMPONENTS[1]));
}
else
{
   http_response_code(404); // Not a "post" request, so invalie URL
}
wrlee
  • 1,595
  • 1
  • 17
  • 25