Enriqe
Enriqe

Reputation: 567

Converting pretty URL into GET parameters

After hours spent by searching with no success I would like to ask for help.

I need to (silently) rewrite this URL: http://example.com/aaa/bbb/ccc
into this: http://example.com/?p1=aaa&p2=bbb&p3=ccc

Basically I need to convert pretty URL into GET params.
My requirements are:

Please help.

What I have now is:
RewriteEngine on
RewriteRule /([^/]+)/([^/]+)?/?([^/]+)?/?([^/]+)?/? ?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]

I wanted to write "this works for maximum 4 parameters" but.... it suddenly stopped work at all and I got 500 Internal Server Error? Aaaarg! I don't know what I'm doing wrong...

Upvotes: 2

Views: 1820

Answers (2)

krzysiej
krzysiej

Reputation: 898

Easier way maybe doing this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA] 

Then in your code you need to just explode $_GET['params'] to get url parts. This work for any numbers of parameters you may have and user will not see the difference because url will always look like example.com/aaa/bbb/ccc

Upvotes: 3

anubhava
anubhava

Reputation: 785246

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# handle 4 parameters
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ ?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]

# handle 3 parameters
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ?p1=$1&p2=$2&p3=$3 [QSA,L]

Upvotes: 2

Related Questions