naser
naser

Reputation: 171

Rewrite URL with 1 or several query in htaccess

I have a URL with query that one of it's query maybe is not set. Target URL:

domain.com/category.php

Query of URL:

1. c = any character in UTF-8 [It is required]
2. page = number [It is not required and maybe not set]

Now, how change URL in .htaccess:

from domain.com/category.php?c=anyCharacter to domain.com/category/anyCharacter/

and from domain.com/category.php?c=anyCharacter&page=number to domain.com/category/anyCharacter/page:number/

UPDATE:

I found an answer but it is not complete.

RewriteRule ^category/(.*)/$ category.php?c=$1 [QSA,L]

This will change URL:

from domain.com/category/anyCharacter/ to domain.com/category.php?c=anyCharacter

and from domain.com/category/anyCharacter/?page=number to domain.com/category.php?c=anyCharacter&page=number

But it's not good answer because I don't want any user see Query in my URL. Please do not write my answer again.

Upvotes: 2

Views: 33

Answers (2)

anubhava
anubhava

Reputation: 784868

You will need 2 rewrite rules for this:

Options -MultiViews
RewriteEngine On
RewriteBase /

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

RewriteRule ^category/([^/]+)/?$ category.php?c=$1 [QSA,L,NC]

RewriteRule ^category/([^/]+)/page:(\d+)/?$ category.php?c=$1&page=$2 [QSA,L,NC]

Upvotes: 1

Wind Mee
Wind Mee

Reputation: 25

So you need to go from pretty URL to variables? You could try something like this:

RewriteEngine On
RewriteBase /
RewriteRule ^([^/\.]+)/?$ category.php?c=$1&page=$2 [QSA,L]

This will translate yourdomain.com/a/2 to yourdomain.com?c=a&page=2

Upvotes: 0

Related Questions