Reda
Reda

Reputation: 711

how to use different url structure in php?

how to use different url structure in php ?
I want to know to handle url like this
www.example.com/10/2011
instead of
www.example.com/?m=10&y=2011

Upvotes: 0

Views: 971

Answers (5)

Sinan
Sinan

Reputation: 5980

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.

Upvotes: 0

RReverser
RReverser

Reputation: 2026

Maybe 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

Upvotes: 2

knittl
knittl

Reputation: 265141

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.

Upvotes: 1

ShinTakezou
ShinTakezou

Reputation: 9661

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)

Upvotes: 0

Adelf
Adelf

Reputation: 706

You should use Rewrite(google it) engine in Apache or nginx. Something like this exists in ISS too.

Upvotes: 0

Related Questions