htaccess trailing slash + redirect fix?
October 6, 2007 9:35 PM   Subscribe

Is it possible to append trailing slashes AND have a URL redirect using .htaccess?

I'm in the middle of migrating domains (both are on the same server, if it helps) and want to redirect http://www.oldname.com/foo/ to http://foo.newdomain.com. In my .htaccess file, I have the following:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^olddomain\.com/foo
RewriteRule (.*) http://foo.newdomain.com/$1 [R=301,L]

And that works really well. However, olddomain.com/foo/ is usually accessed by dropping that trailing slash and being only olddomain.com/foo. Is there any way to get the URL to write the slash in, then redirect to the new domain?
posted by Hot Like Your 12V Wire to Computers & Internet (4 answers total) 1 user marked this as a favorite
 
I'm not sure I understand--are you saying you want to first redirect them to the same URL, but with the trailing slash, and then redirect them again to the new URL? If you're going to redirect your users' browsers to the new domain, why do you care about the trailing slash?
posted by cerebus19 at 9:49 PM on October 6, 2007


Response by poster: I just want to make sure both http://www.olddomain.com/foo AND http://www.olddomain.com/foo/ make it to http://foo.newdomain.com. Right now, the one WITHOUT the trailing slash just 404s.
posted by Hot Like Your 12V Wire at 10:20 PM on October 6, 2007


I don't know what your first Cond does -- looks for a "." in HTTP_HOST (which, I'm thinking, would match just about everything except localhost or some other short name)

If you have a separate VirtualHost container for each domain ...

RewriteEngine On
RewriteRule ^/foo/?(.*) http://foo.newdomain.com/$1 [R]

The /? in there to avoid ending up with a double slash (not that it'd hurt but it's ugly)

(Depending on whether this is in .htaccess or httpd.conf you either get a leading / or not for the Rule, so you may want .... RewriteRule ^/?foo/?....etc)

If that's not any closer to what you need, we might need more details...
posted by devbrain at 10:38 PM on October 6, 2007


The bit I edited out and forgot to put back in -- your second line is using HTTP_HOST but you're also including a path component. You either want to drop it (see my suggestion above) entirely, or split into HTTP_HOST and REQUEST_URI.
posted by devbrain at 10:41 PM on October 6, 2007


« Older Where has all the freeware gone? Mobile freeware...   |   Where should I live in the US while I go to... Newer »
This thread is closed to new comments.