Digital Point Forums
Send Money Online

Go Back   Digital Point Forums > Design & Development > Site & Server Administration > Apache
Google Analytics
Log In to view
your analytics

Reply
 
Thread Tools
  #1  
Old May 13th 2005, 1:51 am
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
mod_rewrite and redirects

Hi,
I've got an old site which I'm in the process of sorting the internal linking out on.
The old url looked like index.php?topic=xxxx
The new url looks like topic/xxxx

So I put a rewriterule in for this:
RewriteRule topic/(.*)$ /index.php?topic=$1 [L]

This works fine, but the old urls are in various links and cached on the SEs, so I want to redirect the old to the new.

I thought the following would do the trick:
RewriteRule topic/(.*)$ /index.php?topic=$1 [L]
RedirectMatch 301 index.php?topic=$1 http://domainname/topic/$1

However, the 301 seems to be doing nothing.
I don't know if what I'm trying to do is even possible without causing a loop of somekind.
Anyone any views/ideas ??

I've also tried RewriteRule index.php?topic=(.*)$ /topic/$1 [R=301, L]
but that gave bad flag deliminator errors.


J
Reply With Quote
  #2  
Old May 13th 2005, 2:34 am
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
getting rid of the space in the [R=301, L] so it reads [R=301,L] got rid of the flag error - but it still seems to do nothing.
Reply With Quote
  #3  
Old May 13th 2005, 10:16 am
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by jlawrence
I thought the following would do the trick:
RewriteRule topic/(.*)$ /index.php?topic=$1 [L]
RedirectMatch 301 index.php?topic=$1 http://domainname/topic/$1
RedirectMatch is not associated with RewriteRule in any way.

Quote:
Originally Posted by jlawrence
However, the 301 seems to be doing nothing.
I don't know if what I'm trying to do is even possible without causing a loop of somekind.
Anyone any views/ideas ??

I've also tried RewriteRule index.php?topic=(.*)$ /topic/$1 [R=301, L]
but that gave bad flag deliminator errors.
The rest of these, you don't have the correct regex in place. Question mark is a special character. What you expression will match is this (? makes 'p' as an optional character):

index.phptopic=...
index.phtopic=...

Escape question mark (as well as other characters - periods, parenthesis, etc) with a slash:

index.php\?topic=(.*)$

J.D.
Reply With Quote
  #4  
Old May 13th 2005, 12:24 pm
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
Quote:
Originally Posted by J.D.
RedirectMatch is not associated with RewriteRule in any way.
didn't mean to say that they were.


Quote:
The rest of these, you don't have the correct regex in place.
I managed to work that out

Quote:
Originally Posted by J.D.
Escape question mark (as well as other characters - periods, parenthesis, etc) with a slash:
index.php\?topic=(.*)$
Escaping characters like ? doesn't make any difference.
Reply With Quote
  #5  
Old May 13th 2005, 12:46 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by jlawrence
Escaping characters like ? doesn't make any difference.
That's how you need to do this anyway. If you are sure that your mod_rewrite works in general, check if you have conflicting .htaccess files in the directory you are trying to access or any other directory above it, besides the one that has .htaccess you are editing.

J.D.
Reply With Quote
  #6  
Old May 13th 2005, 12:57 pm
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
lets just make sure I'm being clear on what I want to do.
I want to do a 301 redirect on a url like /index.php?topic=xxxx to /topic/xxxx where xxx could be any number of things.
I would have thought that the only things that need escaping are the ? and perhaps the =
anyways, to try and avoid any escaping problems, I thought perhaps
RewiteRule index.php(.)topic(.)(.*)$ /topic/$3
but that also has no effect. I'm working on a .htaccess in the root folder (there are no other folders).
Reply With Quote
  #7  
Old May 13th 2005, 1:04 pm
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
fk'it this is giving me a headache.
I'll just write off current IBL's, wait for the site to be spidered again and then start a new link campaign.
There's only about 40 IBL's that I know of, so it's only a day or so's work to replace them.
Reply With Quote
  #8  
Old May 13th 2005, 1:16 pm
AzAkers's Avatar
AzAkers AzAkers is offline
Twilight Vanquisher
 
Join Date: Mar 2005
Location: Phoenix, AZ
Posts: 988
AzAkers is a jewel in the roughAzAkers is a jewel in the roughAzAkers is a jewel in the rough
Try including a ? at the end of the rewritten string - it seems like I remember this same issue with a client a while back - the rewrite would keep failing when it seemed it should work - I guess it was still looking for the variable but adding the ? with no data after it turned out the proper result for us

I'll try to dig up that actual string if I can find it
Reply With Quote
  #9  
Old May 13th 2005, 1:21 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by jlawrence
lets just make sure I'm being clear on what I want to do.
I want to do a 301 redirect on a url like /index.php?topic=xxxx to /topic/xxxx where xxx could be any number of things.
I would have thought that the only things that need escaping are the ? and perhaps the =
anyways, to try and avoid any escaping problems, I thought perhaps
RewiteRule index.php(.)topic(.)(.*)$ /topic/$3
but that also has no effect. I'm working on a .htaccess in the root folder (there are no other folders).
This will do what you need:

RewriteCond %{QUERY_STRING} ^topic=(.*)$
RewriteRule ^index\.php$ /topic/%1? [R=301,L]

J.D.
Reply With Quote
  #10  
Old May 13th 2005, 2:38 pm
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
yep, that did it.
Let me see if I understand what your rewrite will do:
if QUERY_STRING begins with topic= take everything after the topic assign it to variable and apply the rule.
rewrite rule says if url starts with index.php rewrite it to /topic/variable redirected with a 301

That worked and produced the 301. Now what I hoped wouldn't happen does it loops because of the static rewrite to the dynamic
Reply With Quote
  #11  
Old May 13th 2005, 2:40 pm
jlawrence jlawrence is offline
of the Nightfall
 
Join Date: Mar 2005
Location: Plymouth, UK.
Posts: 1,369
jlawrence is a jewel in the roughjlawrence is a jewel in the roughjlawrence is a jewel in the rough
sorted
I've now got this
RewriteCond %{QUERY_STRING} ^topic=(.*)$
RewriteRule ^index.php$ /topic/%1? [R=301,L]

RewriteRule topic/(.*)$ /index.php?jon=1&topic=$1 [L]

Note the extra jon=1& in the final rewrite (this isn't used in any script) but it stops the rules looping

Yipee, no need to disregard those links.
Reply With Quote
  #12  
Old May 27th 2008, 2:14 pm
bozboz bozboz is offline
Peon
 
Join Date: May 2008
Posts: 1
bozboz is on a distinguished road
thanks!

I have been pulling my hair out on this exact same problem for about 4 hours. I dont normally reply on threads but this was a real Eureka moment so thanks very much!

regular expressions is a bit of a nightmare when your just starting!
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -8. The time now is 2:11 pm.