-1

I want to rewrite url from

http://localhost/Ent/Movie/Info.php?year=2011&movie=any_movie

to

http://localhost/Ent/Movie/Info/2011/any_movie

I have tried many code in .htaccess,like

Options +FollowSymLinks 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /Ent/Movie/
RewriteRule ^Info/([0-9]+)/([a-zA-Z0-9]+)$ Info.php?year=$1&movie=$2 [L]

here note that,my .htaccess file is in the root of website.

is my .htaccess location wrong or code inside .htaccess?

iChirag
  • 1,548
  • 17
  • 39
  • http://localhost/Ent/Movie/Info.php?year=2011&movie=any_movie is not rewriting to http://localhost/Ent/Movie/Info/2011/any_movie – iChirag Oct 31 '12 at 18:00
  • I think if the rewrite code is ok then : localhost/Ent/Movie/Info/2011/any_movie will return the result. and localhost/Ent/Movie/Info.php?year=2011&movie=any_movie will not automatically rewrite. – Avin Varghese Oct 31 '12 at 18:07
  • did you enable mod_rewrite locally? http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 – Ryan B Oct 31 '12 at 18:12
  • Be sure to add NC so it is not case sensitive. – James Lawruk Oct 31 '12 at 18:15

2 Answers2

1

To externally redirect a browser to the nicer looking URL (without the query string):

RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /Ent/Movie/Info\.php\?year=([^&]+)&movie=([^&\ ]+)([^\ ]*)
RewriteRule ^ /Ent/Movie/Info/%2/%3?%4 [L,R=301]

Then internally rewrite it back

RewriteRule ^/?Ent/Movie/Info/([0-9]+)/(.*)$ /Ent/Movie/Info.php?year=$1&movie=$2 [L,QSA]

These rules go in the document root, and they'd go right under:

Options +FollowSymLinks 
RewriteEngine on
Jon Lin
  • 135,941
  • 26
  • 200
  • 209
  • what does it mean "externally" and "internally"? – iChirag Nov 01 '12 at 17:37
  • @Downloadmore External means the response sent to the browser or whoever sent the request is "What you want has moved, go over here for it", and it's up to the browser to go fetch it. It is external to the server, the server responds with a redirect and that's the end of story. Internally means the browser sends a request, the server internally (all within the server) rewrites the request and sends it back through its internal URL processing pipeline, maybe at the end of it all, it sends content to the browser, who knows nothing about all the internal redirection – Jon Lin Nov 01 '12 at 18:01
  • @Downloadmore Also see the top part of this answer: http://stackoverflow.com/a/11711948/851273 – Jon Lin Nov 01 '12 at 18:03
0
RewriteRule ^Info/([0-9]+)/([a-zA-Z0-9]+)$ Info.php?year=$1&movie=$2

would match rewrite in the opposite direction, so you need to reverse the logic to something like:

RewriteRule ^Info.php?year=([0-9]+)&movie=([a-zA-Z0-9]+)$ Info/$1/$2

First is what you are trying to match, second argument is the pattern you want to produce. See the mod_rewrite tutorial.

peterph
  • 950
  • 6
  • 11
  • @Petr what should be location of .htaccess file means either in root locaton or Ent folder or in Movie folder – iChirag Oct 31 '12 at 18:16
  • Don't know - check the documentation. :) My guess is, that if you put it into Movie, you could get rid of RewriteBase - but it's just a guess. – peterph Oct 31 '12 at 18:19
  • You can't match against the query string in a rewrite rule – Jon Lin Oct 31 '12 at 23:21