URL with only slashes in pure php like codeigniter

2019-02-28 08:50发布

问题:

I have an application which creates stores for users and they can create a single page store using my dashboard.The final URL generated after they create the store is as follows

baseurl/store/{username}/{random number}

Now how can I access the {username} and {random number}.

If I have a index file in directory 'store' --store ----index.php will I be able to fetch the above username and random number from the url?If this was in codeingiter things would have been easy , and I could make a controller with name store and get the uri segments.But how can I do the same in pure PHP without any frame work.Please do help

回答1:

You have to use a .htaccess in order to do that.

So create this file at the root of your project directory (where index.php is).

.htaccess content :

RewriteEngine On
RewriteRule ^store/([a-zA-Z0-9\_]+)/([0-9]+)$ index.php?username=$1&rand=$2

index.php content:

<?php
var_dump($_GET);
?>

Now try using an url like : mywebsite.com/store/jack_97/354 and you should end up with a dump of $_GET telling you that this array contains two keys : "username" and "rand"