-1

I want to convert this url

http://ts.thebenamorgroup.com/g2g/category.php?cate=mens

to:

http://ts.thebenamorgroup.com/g2g/category/cate/mens/   

I have this rule but it doesnt work:

Options +FollowSymLinks
RewriteEngine on
RewriteRule category/cate/(.*)/ category.php?cate=$1
RewriteRule category/cate/(.*) category.php?cate=$1
Emilio Gort
  • 107
  • 4

3 Answers3

0

You have the logic backwards:

RewriteRule category/cate/(.*)/ category.php?cate=$1
RewriteRule category/cate/(.*) category.php?cate=$1

This will attempt to rewrite category/cate/mens/ into category.php?cate=mens

You can't just reverse it though, because RewriteRule can't operate with query strings, so you need to use RewriteCond as well:

RewriteCond %{QUERY_STRING}     cate=(.*)
RewriteRule /category.php       category/cate/%1

Note the use of %1 to refer to the back-reference in RewriteCond (rather than $1 for a backref in RewriteRule)

fukawi2
  • 5,327
  • 3
  • 30
  • 51
  • i'm trying this,i'll comment – Emilio Gort Jul 09 '13 at 00:28
  • thanks for the answer, I change the code as you suggested, now i'm getting Not Found in the url [link](http://ts.thebenamorgroup.com/g2g/category/cate/womens) – Emilio Gort Jul 09 '13 at 00:47
  • Well it appears the rewrite is working, and that page just does not exist... – fukawi2 Jul 09 '13 at 01:22
  • Does your original question have the URL's the wrong way around? – fukawi2 Jul 09 '13 at 01:23
  • this is the original url [link](http://ts.thebenamorgroup.com/g2g/category.php?cate=mens) and after change my code i type [link](http://ts.thebenamorgroup.com/g2g/category/cate/mens) could you help me to undestard what happen – Emilio Gort Jul 09 '13 at 01:31
  • Well the original URL is being rewritten to the new one correctly... The new one just doesn't exist. It sounds like you're trying to "mask" the original URL with the new URL, is that right? If so, mod_rewrite can't do that, it tells the browser to make a new request for the new URL, so if the new URL doesn't exist, it will 404. – fukawi2 Jul 09 '13 at 05:03
0
RewriteCond %{QUERY_STRING} cate=(.*)
RewriteRule g2g/category.php g2g/category/cate/%1? [L]

This will do exactly what you want. Notice the "?" in RewriteRule stops the query string (?cate=mens) being appended again.

Knoxy
  • 58
  • 5
0

on https://stackoverflow.com/questions/17535520/rewriterule-doesnt-work @JonLin answer my issue and it works, i post here for if somebody need it

Options +FollowSymLinks
RewriteEngine on
RewriteBase /g2g/

RewriteRule ^category/cate/(.+?)/?$ category.php?cate=$1 [L]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /g2g/category\.php\?cate=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /g2g/category/cate/%2?%3 [L,R=301]
Emilio Gort
  • 107
  • 4