Tyler Tervooren
Tyler Tervooren

Reputation: 35

mod_rewrite rule breaks when adding trailing slash 2 subdirectories deep

I'm very, very new to mod rewrite rules, and having trouble with one I'm using to redirect a site previously hosted in a subdirectory to a new domain. Here's the rule I'm using:

RewriteEngine on
RewriteBase /oldsite/ #also tried this without the trailing slash
RewriteRule ^.* http://newsite.com [R=301,NC,L]

This works perfectly until you get 3 levels deep and add a trailing slash to the redirected url. So, the results look like this:

I feel like I'm 99% there, but pulling my hair out a bit figuring out that last little bit. Any idea what I need to change?

Thanks!

Also, I tested placing the .htaccess file in the public html folder as well as in the /oldsite folder and there are no other htaccess files elsewhere on the site.

Upvotes: 0

Views: 178

Answers (1)

bradym
bradym

Reputation: 4961

You're almost there, put this in the .htaccess in /oldsite on your server:

RewriteEngine On
RewriteRule (^.*) http://newsite.com/$1 [R=301,NC,L] 

When you put the regex to match in parenthesis, it becomes available to use as $1. If you have more than one, $1 is the first, $2 the second, etc.

I suggest reading the manual page for mod_rewrite, specifically the rewriterule section (http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule).

Upvotes: 1

Related Questions