0

I have a url:

http://domain.com/i.php?c=PT and I Rewrote it to http://domain.com/PT successfully. But When I browse http://domain.com/i.php?c=PT, it wont redirect to http://domain.com/PT. Is there anyway to both redirect and rewrite it?

My .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^c=([a-zA-Z][a-zA-Z])$
RewriteRule ^/index.php$ /%1%2? [R=301,L]
RewriteRule ^([a-zA-Z][a-zA-Z])$  i.php?c=$1$2 [NC,L]

Thanks

Update #1: I want to redirect from domain.com/i.php?c=PT -> domain.com/PT.

I don't have index.php file.

Kevin Nguyen
  • 189
  • 1
  • 2
  • 8
  • You question is not clear. What I understand is that you succesfully redirected A to B but when you browse A you are not redirected to B. Does it work or not? – ColOfAbRiX Apr 23 '15 at 21:40
  • I meant when I browse http://domain.com/PT, it works for me. But when I browse http://domain.com/i.php?c=PT, it keeps the same url instead of redirecting to http://domain.com/PT. – Kevin Nguyen Apr 24 '15 at 07:33
  • Ok, so you made a type in your question :) Can you amend it adding also the relevant Apache configuration? – ColOfAbRiX Apr 24 '15 at 09:41
  • No, I can't. It is shared hosting. – Kevin Nguyen Apr 24 '15 at 10:11
  • There are a lot of Rewrite topics but this is specific. – Kevin Nguyen Apr 24 '15 at 10:46
  • Sorry, not clear. Do you need to rewrite `domain.com/PT -> domain.com/i.php?c=PT` or `domain.com/i.php?c=PT -> domain.com/PT`? Are there other URL formats you need to translate? In both cases your rewrite rules are wrong. – ColOfAbRiX Apr 24 '15 at 11:28
  • @KevinNguyen It also contains information about how to debug rewrite rules, starting by turning logging on and then reading the logs. – Jenny D Apr 24 '15 at 12:03
  • @ColOfAbRiX I want to rewrite from domain.com/i.php?c=PT -> domain.com/PT. – Kevin Nguyen Apr 24 '15 at 13:38
  • 1
    The linked question is a [canonical](http://meta.serverfault.com/q/1986/55514) one. These are questions where the community has said all it is going to say on a class of subject, because although everyone's particular problems in that class are somewhat different, to the extent that they are interesting, they aren't different, and to the extent that they're different, they aren't interesting (to anyone save the questioner). So we write one set of answers designed to be the last word on the subject, and say no more. – MadHatter Apr 24 '15 at 14:50

1 Answers1

0

I'm not sure what behavior do you want, I see no mention of index.php in your request, but it is in your configuration. Anyway this rewrites any URL from the form hostname/XX to hostname/i.php?c=XX

RewriteEngine On
RewriteRule ^/([a-zA-Z]{2})$  /i.php?c=$1 [R=301,NC,L]

For the opposite translation:

RewriteCond %{QUERY_STRING}  ^c=([a-zA-Z]{2})$
RewriteRule ^/i.php$         /%1? [R=301,NC,L]

And you can debug your rewrite rules adding:

RewriteEngine On
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 3
ColOfAbRiX
  • 980
  • 2
  • 11
  • 22
  • To be honest, there is no index.php file. I redirected index.php. – Kevin Nguyen Apr 24 '15 at 13:32
  • These rules didn't work for me. I got 404 when browse domain.com/PT. To make it clear, i want to rewrite domain.com/i.php?c=PT -> domain.com/PT. Whenever people type domain.com/i.php?c=PT -> it will be domain.com/PT on the address bar. Thanks! – Kevin Nguyen Apr 24 '15 at 13:43
  • Is `domain.com/PT` a valid URL? Does that resource exists? If it doesn't that's why you get 404. Usually people do the opposite, they translate a human readable URL like `domain.com/articles/myarticle` to `domain.con/articles.php?page=myarticle` – ColOfAbRiX Apr 24 '15 at 14:25
  • The resource should be domain.com/i.php?c=PT. And I want to rewrite to domain.com/PT. And when visitors type on their browser domain.com/i.php?c=PT, it will rewrite to domain.com/PT on their address bar. That is my goal. – Kevin Nguyen Apr 24 '15 at 14:51