Roy
Roy

Reputation: 987

Rewrite URL with 1 or more parameters?

I'm strugglening with my .htaccess file in orther to achieve this:

a.com/male-items  (OR)     
a.com/male-items/popularity   ->    a.com/index.php?g=m&sort-popularity 

a.com/female-items  (OR)     
a.com/female-items/popularity ->    a.com/index.php?g=f&sort=popularity 

a.com/male-items/alphabet     ->    a.com/index.php?g=m&sort=alphabet
a.com/male-items/alphabet/a   ->    a.com/index.php?g=m&sort=alphabet&l=a    
(and same for female)

I know it should be something like

RewriteRule ^a$ a.com/index.php?q=$1

But actually looking into the different mod-rewrite / regex explanations and cheat-sheets doesn't help a lot with getting it to work. The hard part is to understand how do you define the different parametes in the address and then use them in the rewritten url.

(any explanations with your solution would be appretiated)

Upvotes: 0

Views: 57

Answers (2)

anubhava
anubhava

Reputation: 786091

Use these rules in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^male-items/?$                   /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$           /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$   /index.php?g=m&sort=$2&l=$3 [L,QSA]

RewriteRule ^female-items/?$                 /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$         /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]

Upvotes: 1

SAnDAnGE
SAnDAnGE

Reputation: 428

These htaccess lines redirect all nonexisting files/folders to index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.

Upvotes: 0

Related Questions