htaccess redirect problem
October 21, 2010 5:30 PM   Subscribe

Newbie .htaccess problem

How do I rewrite

http://server.example.com/~mysite/foo

to

http://mysite.example.com/foo/

using .htaccess?

I've tried using this code:

RewriteEngine on
RewriteCond %{http_host} ^server\.example\.com/~mysite [NC]
RewriteRule ^(.*)$ http://mysite.example.com/$1 [R=301,NC]

But without success. What gives?
posted by Paragon to Computers & Internet (18 answers total) 1 user marked this as a favorite
 
Are you seeing the 301 come back from the server at all? A tool like LiveHTTPHeaders (or its IE equivalent), or even lwp-request would show some of the gory details.
posted by jquinby at 5:39 PM on October 21, 2010


Response by poster: Using LiveHTTPHeaders, no redirect is coming through - pages are just registering HTTP/1.1 200 OK.
posted by Paragon at 5:58 PM on October 21, 2010


This is pretty basic, but Apache allows directory-level control of what .htaccess files can do. It's possible that your host has this functionality turned off. It's also possible that the rewrite module is not installed. You should probably ensure that what you're trying to do is possible before you beat yourself up too much.
posted by richyoung at 6:16 PM on October 21, 2010


Off the top of my head, I think this:

RewriteEngine On
RewriteCond %{SERVER_NAME} ^server\.example\.com$
RewriteRule ^/~([^\/]+)?/(.*) http://$1.example.com/$2 [R=301]
posted by Monsieur Caution at 6:19 PM on October 21, 2010


Incidentally, mistakes in your version include looking for the user directory part of the request in HTTP_HOST and redirecting /~mysite/foo to http://mysite.example.com/~mysite/foo.
posted by Monsieur Caution at 6:22 PM on October 21, 2010


Another trick to get at the nuts and bolts is to turn on debugging, either at the server level or in the VirtualHost container with something along the lines of this:


RewriteLog /some/path/to/logs/rewrite.log
RewriteLogLevel 5

Another gotcha is that AllowOverride has to be set to All for that directory, a la:


Options FollowSymLinks
AllowOverride All
DirectoryIndex index.html


I think the FollowSymLinks also has to be enabled. (ug, mefi is eating all of my directory container tags)
posted by jquinby at 6:28 PM on October 21, 2010


Response by poster: I have single (1 url to 1 url) rewrites working already through htaccess - could the host have turned off wildcard functionality only?

I should clarify that "~mysite" is not identical to "mysite"; perhaps this is more accurate:

http://server.example.com/~mysite/foo/
to
http://othersite.example.com/foo/

I have FollowSymLinks enabled, I'm pretty sure.
posted by Paragon at 6:30 PM on October 21, 2010


Even simpler:

RewriteEngine On
RewriteCond %{SERVER_NAME} ^server\.example\.com$ [C]
RewriteCond %{REQUEST_URI} ^/~mysite/
RewriteRule ^/~mysite/(.*) http://othersite.example.com/$1 [R=301]

What's happening in your example is there's no match on /~mysite in %{HTTP_HOST} because the user directory is never in that variable. It's in %{REQUEST_URI} or %{SCRIPT_NAME}.
posted by Monsieur Caution at 6:36 PM on October 21, 2010


Response by poster: There must be a syntax error somewhere in my code - plugging that in gives a 500 Server Error for all pages on the site.
posted by Paragon at 6:52 PM on October 21, 2010


I don't see a typo there, but to avoid them, you can boil this down to ...

RewriteEngine On
RewriteRule ^/~mysite/(.*) http://othersite.example.com/$1 [R=301]
posted by Monsieur Caution at 6:55 PM on October 21, 2010 [1 favorite]


Response by poster: Well, it's not 500ing, but doesn't appear to be rewriting either. This might be a stupid question, but is this an issue of where the .htaccess is located? I'm editing here:

http://server.example.com/~mysite/.htaccess

But does the file need to be here instead to be effective?

http://server.example.com/.htaccess
posted by Paragon at 7:04 PM on October 21, 2010


Hm, it's true that I rarely do anything with .htaccess files down in user directories.

I suspect by that point the /~mysite part has already been translated to some other form, and your rewrite rule needs to operate on that. To see what you're really dealing with, just create a subfolder to test in and redirect to a deeper, non-existent subfolder, e.g. ...

RewriteEngine On
RewriteRule ^(.*) http://server.example.com/~mysite/$1 [R=301]

Even if you have a custom 404 or something like that getting in the way of the redirect, Live HTTP Headers will now show you what you're working with. You can do the same to print out %{REQUEST_URI} and so on and quickly see what Apache sees right at the moment your rule is being processed.
posted by Monsieur Caution at 7:19 PM on October 21, 2010


Response by poster: I'll have to try this out on Tuesday, heading off on the long weekend now. But thank you very much for the suggestions - I feel like I have a better grip on what's going on, even if I don't have a solution yet!
posted by Paragon at 8:07 PM on October 21, 2010


What goes in the file depends on what directory you put it in. The stuff that RewriteRule matches against is relative to the directory that the htaccess file is in. If you are trying to match ~/dir/foo from ~/dir/.htaccess then your RewriteRule should start with just /foo because everything before that is stripped off. If you want everything under ~/dir/ to redirect then just put a htaccess in that dir that redirects everything. It will only apply to requests that have already matched up to the ~/dir/ part.
posted by Rhomboid at 8:57 PM on October 21, 2010 [1 favorite]


Also, your original RewriteCond is nonsensical because you're matching against ${http_host} which is just the hostname of the request, but you're trying to match a host and path which will never match.
posted by Rhomboid at 9:00 PM on October 21, 2010


I'd be with Monsieur Caution, but...

RewriteEngine On
RewriteCond %{SERVER_NAME} ^server\.example\.com$ [C]
RewriteCond %{REQUEST_URI} ^([/]*)~mysite/
RewriteRule ^([/]*)~mysite/(.*) http://othersite.example.com/$2 [R=301,QSA]


The ([/]*) because one of my machines can't decide if it wants the leading slash or not; the $2 because there's now the ([/]*) in there; and the QSA so that any GET variables get passed on as well.
posted by sodium lights the horizon at 6:34 AM on October 22, 2010


That would only be appropriate for a section in httpd.conf or in a .htaccess in the root dir, not for a .htaccess in /~mysite. Again, the path that you match against is relative to the location of the htaccess file. Also, there's no reason to check for /~mysite in the path twice, so the second RewriteCond is superfluous.
posted by Rhomboid at 2:38 PM on October 22, 2010


(And OP is editing the .htaccess file in /~mysite, which is why I bring it up.)
posted by Rhomboid at 2:39 PM on October 22, 2010


« Older How to avoid hiring manipulative people   |   A little tast of Shanghai (without the pork)? Newer »
This thread is closed to new comments.