-Dave- Posted April 6, 2010 Share Posted April 6, 2010 Hi all - going out my tree here. I'm trying to 301 redirect hostname.com/articles.php?id=500 to hostname.com/articles/500/ But none of these work: RewriteBase / RewriteRule ^articles.php?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L] RewriteRule ^articles\.php?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L] RewriteRule ^articles\.php\?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L] RewriteRule ^articles\.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L] RewriteRule ^articles.php\?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L] RewriteRule ^articles.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L] What am I doing wrong ? I tried this service: http://civilolydnad.se/projects/rewriterule/ and it said that ^articles.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L] should work, but it's not :( any suggestions? thanks Link to comment Share on other sites More sharing options...
0 +theblazingangel MVC Posted April 6, 2010 MVC Share Posted April 6, 2010 The third one should be correct, '.' and '?' are special characters in regex, '=' is not! The problem is possibly due to the RewriteBase of '/' coupled with the start of line marker ('^') at the start of the rule, try removing the '^'! i.e. RewriteEngine on RewriteBase / RewriteRule articles\.php\?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L] Link to comment Share on other sites More sharing options...
0 -Dave- Posted April 6, 2010 Author Share Posted April 6, 2010 nope, this does not work either :( Link to comment Share on other sites More sharing options...
0 +theblazingangel MVC Posted April 6, 2010 MVC Share Posted April 6, 2010 Just to be clear 1) you've got other rewrites that work don't you... So you're sure rewriting is turned on and configured correctly, yes... 2) you are converting it back again to article.php aren't you... If not, it's obviously not going to work. And if so, when you say it doesn't work, are you absolutely certain that it's this bit that's not working? Or could it potentially be another bit... (if so give us the rest) edit: [R=301,L,NC] might be a good idea btw... Link to comment Share on other sites More sharing options...
0 -Dave- Posted April 6, 2010 Author Share Posted April 6, 2010 hey, sorry. yes, rewrites do work :) what I am doing is 301 rewriting all the old URLs from an old site, to an app coded in an MVC framework. Obviously google doesnt like pages with duplicate content, so I am 301'ing all the old links. all the sites rewrites are finally processed with these lines: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/forum # Rewrite all other URLs to index.php/URL RewriteRule ^(.*)$ index.php?$1 [QSA,L] When i say it doesnt work, the URL stays in the old format (it hasnt redirected), and just shows the generic 404 page from my APP (as expected if no route is found) Thanks :) Link to comment Share on other sites More sharing options...
0 Mike Posted April 6, 2010 Share Posted April 6, 2010 RewriteRule doesn't match with the query string so.. RewriteEngine On RewriteBase / RewriteCond %{QUERY_STRING} ^id=(.*)$ RewriteRule ^articles\.php$ articles/%1? [R=301,L] does http://example.com/articles.php?id=foo to http://example.com/articles/foo. That obviously only works if the id is at the start of the query string and no other variables are set. Link to comment Share on other sites More sharing options...
0 Calculator Posted April 11, 2010 Share Posted April 11, 2010 If you want to redirect the browser, then shouldn't you be using RedirectRule instead of RewriteRule? Link to comment Share on other sites More sharing options...
0 Ahmed Samir Posted April 11, 2010 Share Posted April 11, 2010 Forget about RewriteBase, sometimes it's just a trouble maker. RewriteEngine On RewriteRule ^articles/([0-9]+)/$ articles.php?article=$1 [L] It's that simple. Link to comment Share on other sites More sharing options...
0 Ahmed Samir Posted April 27, 2010 Share Posted April 27, 2010 If you want to redirect the browser, then shouldn't you be using RedirectRule instead of RewriteRule? There is no such thing as RedirectRule, just FYI :-) "RewriteRule" or "Redirect 301" are what you can use. In his case, RewriteRule is the right method. Cupcakes 1 Share Link to comment Share on other sites More sharing options...
0 +theblazingangel MVC Posted April 28, 2010 MVC Share Posted April 28, 2010 RewriteEngine On RewriteRule ^articles/([0-9]+)/$ articles.php?article=$1 [L] It's that simple. Just FYI, you do realise that's a solution to the wrong problem don't you. We're not converting article/500/ to article.php?id=500, we're doing the unconventional, we're doing it the other way around! The point is to redirect a set of old links to new ones at the new site. Link to comment Share on other sites More sharing options...
0 Ahmed Samir Posted April 28, 2010 Share Posted April 28, 2010 Just FYI, you do realise that's a solution to the wrong problem don't you. We're not converting article/500/ to article.php?id=500, we're doing the unconventional, we're doing it the other way around! The point is to redirect a set of old links to new ones at the new site. Eeek, I didn't know that, apparently I got confused. Sorry about that :whistle: Link to comment Share on other sites More sharing options...
Question
-Dave-
Hi all - going out my tree here.
I'm trying to 301 redirect hostname.com/articles.php?id=500 to hostname.com/articles/500/
But none of these work:
RewriteBase /
RewriteRule ^articles.php?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
RewriteRule ^articles\.php?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
RewriteRule ^articles\.php\?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
RewriteRule ^articles\.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
RewriteRule ^articles.php\?id=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
RewriteRule ^articles.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
What am I doing wrong ? I tried this service:
http://civilolydnad.se/projects/rewriterule/
and it said that
^articles.php\?id\=(.*)$ http://hostname.com/articles/$1/ [R=301,L]
should work, but it's not :(
any suggestions?
thanks
Link to comment
Share on other sites
10 answers to this question
Recommended Posts