Mod_Rewrite not working


#1

Hello,

I am trying to do the following:

http://www.domain.com/index.php?newsid=5504

When this url is visited I want it to be re-written as:

http://www.domain.com/dev/index/newsid/5505/

I have this in my .htaccess file and it is not working

Options +FollowSymLinks
RewriteEngine on

RewriteRule index/newsid/(.*) index.php?newsid=$1

Any help would be majorly appreciated :slight_smile:


#2

you might be misunderstanding how mod_rewrite works. if you follow a link to http://www.domain.com/index.php?newsid=5504 (or enter that in the address bar), it checks /index.php against your rewrite rules and finds no match so leaves it alone.

on the other hand, if you go to http://www.domain.com/index/newsid/5505/ it checks /index/newsid/5505/ and finds a match in index/newsid/(.*) and rewrites it to index.php?newsid=5505/

that should work on dreamhost’s servers, but on my own local installation since there is an index.php and the url starts with index/ i have seen it actually run index.php with nothing in $_GET but /index/newsid/5505/ available in $_SERVER somewhere. i’ve seen this done on purpose but usually the .php is included in those cases.

i’d also recommend doing something a little more friendly than index/newsid/5505/ for the url, like news/5505 for example. of course it has to be something that’s not going to collide with any existing urls.

track7 - my dream-hosted site


#3

i forgot to mention if you want to actually change the url when someone visits /index.php?newsid=5504 i’d recommend handling that in php. check $_SERVER[‘REQUEST_URI’] to see that it starts with /index.php?newsid= and then grab the numbers and do header(‘Location: http://www.domain.com/index/newsid/5504/’); die; to get the browser to request your desired url.

track7 - my dream-hosted site