Okay, so after spending about three days trying to figure out mod_rewrite, I'm pretty sure I've gone in the wrong direction On the plus side I now kind of understand mod_rewrite. I'm trying to 301 redirect a dynamic url of the form www.domain.com/index.php?pg=785 to www.domain.com/topic/optional/optional Now as far as I can tell mod_rewrite will only allow me to change www.domain.com/index.php?pg=785 to www.domain.com/785, which unfortunately is not what I want Can anybody tell me how (or at least show me the right direction) to point my different page ids to a new static url? *edit*Also I need to change sub.domain.com/index.php?pg=786 to go to sub.domain.com/topic/optional/optional, because I imagine that'll be slightly different too.*edit* All help appreciated.
Wait, thinking about it. If I did mod_rewrite them all to www.domain.com/xyz, could I then just do a standard 301 from old to new, like redirect 301 /xyz.html http://www.domain.com/topic/optional/optional.html? If so, how slow would that make answering queries if the server had to work all that out?
If you have a small list of redirects/rewrites you want then you could just each one on it's own line. RewriteRule ^/index.php?pg=785 /topic/optional/optional [R=301,NC,QSA,L] RewriteRule ^/index.php?pg=786 /topic/optional/optional2 [R=301,NC,QSA,L] Code (markup): However I don't think that's what you're actually asking for. If you want a rewrite rather than a redirect, you can have the URLs displayed in your user's browsers look like /topic/optional/optional. The trouble is, Apache can't do this alone unless you give it all the information it needs. This means adding one line for every separate URL -> pg code lookup you have. This is obviously less than ideal. On the other hand... if you want to use PHP as well, then it becomes much easier. The way I have achieved this is either for my blog URLs: RewriteRule ^/blog/([0-9]{4}/[0-9]{2}/[0-9]{2}) /blog/index.php?date=$1 [QSA] Code (markup): Then I simply use PHP to convert the date into a timestamp and look it up in the database. For my article URLs: RewriteRule ^/articles/([a-zA-Z0-9_-]+)$ /articles/index.php?articlecode=$1 [QSA,L] Code (markup): I use pretty much the same methodology except that I use the text part of the URL as the lookup rather than the date. For your situation, I would probably use something like: RewriteRule ^/[a-z]+(/[a-z]+)*/([0-9])+ /index.php?pg=$2 [NC,QSA,L] Code (markup): which does not exactly match the URLs you specified but instead has the pg code appended to the end. This way you have a nice, easy integer to look up in your database.
Okay, I have a couple of smaller sites that are only 10-15 pages that I'm moving as well, so I thought I'd try your first suggestion. Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteRule ^/index.php?pg=8 /index.html [R=301,NC,QSA,L] Code (markup): Which didn't work, so I then tried to put the URL in full Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteRule ^/index.php?pg=8 http://www.mydomain.com/index.html [R=301,NC,QSA,L] Code (markup): Which also didn't work. Am I missing something? The rewrite engine is definately on.
If you're putting this in a .htaccess file, you need to remove the leading slash from the regex part of the rewrite rule. RewriteRule ^index.php?pg=8 /index.html [R=301,NC,QSA,L] Code (markup): Although that rewrite looks kinda backwards to me. It would be more normal to have: RewriteRule ^index.html /index.php?pg=8 [NC,QSA,L] Code (markup): Which would leave index.html in the URL bar of the browser but display the content from index.php?pg=8.