2

I'm trying to redirect all URLs past the "/" of my domain to an old.mysite.com version, but leave the mysite.com itself alone.

For example, "mysite.com" -> "mysite.com", but "mysite.com/some-url" -> "old.mysite.com/some-url". Here's what I have now:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteRule /(.*) http://archived.example.com/$1

But it's redirecting "mysite.com" -> "old.mysite.com". How can I modify this so that the core domain doesn't get redirected, but only when there are URL bits past the ".com/" so to speak?

1 Answer 1

0

The RewriteCond %{REQUEST_URI} ^/(.+)$ matches all characters after the /

(.+) is a regular expression that matches any character.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9\/-]+$
RewriteRule /(.*) http://archived.example.com/$1

The above might do it, but I'm not very good with regular expressions.

[a-zA-Z0-9\/-]+ will match a series of any of the characters in the brackets following the trailing /.

I use RegExLib as my cheat-sheet. It'll also let you test regular expressions against a source (to see what matches).

4
  • Nathan - Thanks for the response. From my understanding, (.+) matches any character (but must be at least 1 character). My thinking was that I didn't want to redirect to archived.example.com unless there were characters after the slash, because I wanted just "example.com" to stay "example.com" if that makes sense. Thanks again for helping...
    – rpheath
    Commented Dec 12, 2011 at 0:53
  • It makes perfect sense. And I've confused myself. Will revisit a bit later.
    – Nathan
    Commented Dec 12, 2011 at 0:59
  • Nathan, you steered me on the right track by pointing out that I was matching everything (including spaces). I fixed my regex to read: RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9\/-]+$ and all is working now. Thanks!
    – rpheath
    Commented Dec 12, 2011 at 1:12
  • That's great! I learned a little something, too! I've edited my answer, above, with your contribution, so maybe if someone else comes along it'll be helpful! I thought about including [a-zA-Z0-9\/-]+, but what confused me was whether we should be matching the entire URL or only what followed the trailing /. If I'd read the documentation more closely, it might have helped! Glad we got it figured out!
    – Nathan
    Commented Dec 12, 2011 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.