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.

9
  • 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, 2015 at 21:40
  • I meant when I browse domain.com/PT, it works for me. But when I browse domain.com/i.php?c=PT, it keeps the same url instead of redirecting to domain.com/PT. Apr 24, 2015 at 7:33
  • Ok, so you made a type in your question :) Can you amend it adding also the relevant Apache configuration?
    – ColOfAbRiX
    Apr 24, 2015 at 9:41
  • No, I can't. It is shared hosting. Apr 24, 2015 at 10:11
  • 1
    The linked question is a canonical 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, 2015 at 14:50

1 Answer 1

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
4
  • To be honest, there is no index.php file. I redirected index.php. Apr 24, 2015 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! Apr 24, 2015 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, 2015 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. Apr 24, 2015 at 14:51

Not the answer you're looking for? Browse other questions tagged .