0

I have an page with multiple parameters. Is it possible to change the url's like below using htaccess or using php.

http://example.com/result.php?year=2012
Change To
http://example.com/year-2012

http://example.com/result.php?category=naturals
Change To
http://example.com/category-naturals

http://example.com/result.php?year=2012&category=naturals
Change To
http://example.com/year-2012/category-naturals

3 Answers 3

1

You can do it using .htaccess. Try something like this:

RewriteRule ^year-(.*)  result.php?year=$1 [PT]
RewriteRule ^category-(.*)  result.php?category=$1 [PT]
RewriteRule ^year-(.*)/category-(.*)  result.php?year=$1&category=$2 [PT]
Sign up to request clarification or add additional context in comments.

1 Comment

solution for single parameter is working perfect. But the solution for 2 parameters is not working perfect. It's takes as single parameter and I get following when I print request values. Array ( [year] => 2012/category-2013 ) I must need to study regular expression
0
(http:\/\/.*\/).*?\?(?:(\w+)=(\w+)&?)*

Try this.Replace by $1$2-$3.See demo.

http://regex101.com/r/yA1jY6/2

Comments

0

You can have these rules in your root .htaccess:

RewriteRule ^year-([^/]+)/?$                  result.php?year=$1             [NC,L,QSA]
RewriteRule ^category-([^/]+)/?$              result.php?category=$1         [NC,L,QSA]
RewriteRule ^year-([^/]+)/category-([^/]+)/?$ result.php?year=$1&category=$2 [NC,L,QSA]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.