how to use different url structure in php?
how to use different url structure in php ?
I want to know to handle url like 开发者_C百科this www.example.com/10/2011 instead of www.example.com/?m=10&y=2011Maybe it will be better to do with server configuration - .htaccess (in Apache) or web.config (in IIS). Create rewrite rule from ^(\d+)/(\d+)$ to /?m=$1&y=$2. For example, in Apache it will look like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\d+)/(\d+)$ /?m=$1&y=$2
this can be achieved through apache's mod_rewrite mechanism. create a .htaccess
file in your webroot with the following contents:
RewriteEngine on
RewriteRule ^([0-9]+)/([0-9]+) ?m=$1&y=$2 [NC]
similar mechanisms exist for other web servers.
You should use Rewrite(google it) engine in Apache or nginx. Something like this exists in ISS too.
This is not achieved by PHP, but by some other "component". E.g. the Apache web server has a module (the rewrite engine) that can rewrite the URL so that you can use URL like www.example.com/10/2011
and it translates them into www.example.com/?m=10&y=2011
so that in your PHP code you can use $_GET['m'] and get the month. Rewrite engine for Apache (for other webserver similar things may exist)
One easy method is something like this:
In your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
What this does it redirects everyhting to index.php if its not a physical file or dir.
Then you configure your index.php to handle this request.
Example
www.example.com/10/2011
will become $_GET['10/2011']
;
so you can use data in your script.
精彩评论