• 0

301 Rewrite Help


Question

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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.

Link to comment
Share on other sites

  • 0

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.