Slay
Slay

Reputation: 1283

My simple rewrite rule does not work

RewriteRule ^(.*)/(.*)/$ index.php?r=contest&id=$1

I have want to rewrite

localhost/index.php?r=contest&id=1

to

localhost/contest/1

Above is the rewrite rule that i came out. Initially i thought it will work, but apparently it does not. can anyone guide me on the mistake?

Cheers

Upvotes: 0

Views: 824

Answers (2)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

The rule in your question is using back reference $1 to contest in the first regex group, instead of $2 to the 2nd group with 1.

You may try this instead:

RewriteRule ^([^/]+)/([^/]+)/? index.php?r=contest&id=$2 [L,NC]

Or:

RewriteRule ^([^/]+)/([^/]+)/? index.php?r=$1&id=$2 [L,NC]

OPTION:

The rule using only one parameter should be like this, assuming it is a dynamic string:

RewriteRule ^([^/]+)/? index.php?r=$1 [L,NC]

UPDATE with a complete rule-set:

Request example: localhost/contest/1

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]

# 2 parameters. Both dynamic.
RewriteCond %{REQUEST_URI} !index\.php   [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?r=$1&id=$2 [L,NC]

# 1 dynamic parameter.
RewriteCond %{REQUEST_URI} !index\.php   [NC]
RewriteRule ^([^/]+)/?$ index.php?r=$1   [L,NC]

Upvotes: 1

STiTCHiCKED
STiTCHiCKED

Reputation: 526

here is a really good doc on cleaning your url's

Upvotes: 0

Related Questions