Struggling with Apache 301 redirect

Ok, so I am trying to write a 301 redirect with apache mod rewrite, here’s what I have:

## 301 Redirects
# 301 Redirect 1
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)$ http://www.purplewidgets.net$1 [R=301,NE,NC,L]
RewriteRule ^uk-widgets(.*)$ http://uk.purplewidgets.net$1 [R=301,NE,NC,L]
RewriteRule ^ireland-widgets(.*)$ http://ireland.purplewidgets.net$1 [R=301,NE,NC,L]
RewriteRule ^about-purple-widgets(.*)$ http://www.purplewidgets.net/about-purple-widgets$1 [R=301,NE,NC,L]
RewriteRule ^widgets-search(.*)$ http://www.purplewidgets.net/widgets-search$1 [R=301,NE,NC,L]
RewriteRule ^contact-purple-widgets(.*)$ http://www.purplewidgets.net/contact-purple-widgets$1 [R=301,NE,NC,L]
RewriteRule ^site-map(.*)$ http://www.purplewidgets.net/site-map$1 [R=301,NE,NC,L]

This ‘sort of’ works, however if I enter current URL without a trailing /, it the redirect doesn’t work correctly, for example:

http://www.currentsite.co.uk/uk-widgets goes to http://www.purplewidgets.net/uk-widgets - it should go to the specified subdomain on the new site.

Conversely, if I do enter a trailing slash it goes to a URL with a double slash, e.g:

http://www.currentsite.co.uk/uk-widgets goes to http://uk.purplewidgets.net//

What am I doing wrong?

P.S, I’m not very technical so I’ve had to pull this together from the bits and pieces I could find on the net.

Thanks!

Jonny,

You’ve used the :kaioken: EVERYTHING :kaioken: atom which is redirecting EVERYTHING to the www subdomain. Go figure.

Regards,

DK

Thanks DK.

Could you please clarify which is the ‘everything’ part you are referring to? I’m in over my head here. :slight_smile:

The link to the mod_rewrite article in DK’s signature isn’t working :frowning: but there’s a SitePoint tutorial you can read.

I think the bit he’s referring to is:

A very common approach is to use the expression (.). This expression combines two metacharacters: the dot character, which means ANY character, and the asterisk character, which specifies zero or more of the preceding character. Thus, (.) matches everything in the {REQUEST_URI} string. {REQUEST_URI} is that part of the URI which follows the domain up to but not including the ? character of a query string, and is the only Apache variable that a rewrite rule attempts to match.

Wrapping the expression in brackets stores it in an “atom,” which is a variable that allows the matched characters to be reused within the rule.

Thanks, TB! My server had DNS problems for a few hours but the site is back up.

Next time, though, please add the appropriate warnings (about the abuse I heap on newbies for not knowing the dangers of (.*), i.e., inappropriate use … if no other reason!).

Jonny,

I was referring to your

RewriteRule ^(.*)$ http://www.purplewidgets.net$1 [R=301,NE,NC,L]

line.

Question (so I know that you’re not a “script kiddie”): Why are you using No Escape and No Case flags in your RewriteRule (especially when you’re capturing EVERYTHING in the {REQUEST_URI})?

Regards,

DK

vB is messed up - not able to edit the last post.

Thank you, TechnoBear! Indeed, there was something wrong at my website beyond the DNS problem as the .htaccess file had been changed (unbeknownst to me). That’s been corrected (and tested) and the link to the Tutorial Article is alive and well again.

Regards,

DK

Thanks for all your help guys. Just one last question:

Let’s say I have a URL like this:

http://www.domain.com/sub-folder/other/folder/etc

If I wanted to redirect that URL to:

http://www.newdomain.com/other/folder/etc.

The second one copies the original path, but omits '/sub-folder/. Any idea how I can do this?

Thanks!

Jonny,

Sure, the change folder in the sample code of the tutorial Article linked in my signature works.

Regards,

DK