Madara
Madara

Reputation: 22

rewrite URL using .htaccess only works on first query string

I'm trying to rewrite my url using htaccess but it only worked on the first query string

what I want is to rewrite this URL

acnologia.com/index.php?page=images&tag=nature

into something like this acnologia.com/images/nature

it does work on the first query string acnologia.com/images but doesn't work if i add another directory after "images"

this is my .htaccess code:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2

Upvotes: 0

Views: 260

Answers (2)

chanchal118
chanchal118

Reputation: 3657

The very basic rule will be like this

RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2

And more specific will be

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2

which @Andrew answered in comment.

Your final rewrite rule should be following. RewriteCond is specified so that js, css, png etc. files do not match this rewrite rule.

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]

This tutorial is helpful.

Upvotes: 0

Andrew
Andrew

Reputation: 5340

There's only one group in your original rule, so there's no $2.

Try this one:

RewriteRule ^([a-z\d_-]+)/([a-z\d_-]+)$ index.php?page=$1&tag=$2 [NC,L] 

\d equals 0-9, and NC means nocase

Upvotes: 0

Related Questions