Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

301 redirect - best way?

i've been reading about 301 redirects which talk about modifying either .htaccess or httpd.conf but none of the suggestions i've found are working... my goal: if someone enters example.com, i want the server to change the url to www.example.com
i've read server admin does not do 301 permanent redirects. does anyone know the best way to do this? thanks.

Mac OS X (10.5.2)

Posted on Mar 22, 2008 9:35 AM

Reply
14 replies

Mar 26, 2008 2:46 PM in response to J R3

Server Admin > Sites > sitename > Options > Allow All Overrides was unchecked, I checked it but still no change. Strange thing is, even after checking this box and saving, if I look at /etc/httpd/httpd.conf with vi, there's this line still:

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

I tried manually changing None to All and still a no go. Arrrgggh.

Mar 26, 2008 3:09 PM in response to Scott Joing

looks like you're running leo server... which (unless upgraded from tiger server) runs Apache2 by default. (Check to see what version of apache you're using... go to domain.com/skdjkjdn to see an error page which will tell you).

If you have apache2, your config file is in something like /etc/apache2/

NOT in /etc/httpd ... that is all apache 1.3 stuff

when u open your conf file in vi, scroll all the way to the bottom, as that is where server admin writes its settings.

if the following setting appears twice in ur config file:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

only the last instance of it will be used

Mar 26, 2008 3:33 PM in response to J R3

okay, looks like we are on the right track. i found this in /etc/apache2/sites/myserver_80.conf:

<Directory "/Library/WebServer/Documents">
Options All -Includes -ExecCGI +MultiViews -Indexes
AllowOverride All
<IfModule mod_dav.c>
DAV On
</IfModule>
</Directory>

and changed it to this:

<Directory "/Library/WebServer/Documents">
Options All +FollowSymLinks -Includes -ExecCGI +MultiViews -Indexes
AllowOverride All
<IfModule mod_dav.c>
DAV On
</IfModule>
</Directory>

saved, restarted, but still doesn't work! hmmm.

Mar 26, 2008 8:53 PM in response to J R3

yes, i've been looking there too. this is the only reference to the / directory in /etc/apache2/httpd.conf

<Directory "/">
Options FollowSymLinks
AllowOverride All
</Directory>

there are not any AllowOverride statements near the end of the file (which is where Server Admin is supposed to be writing?). the only other AllowOverride statements in /etc/apache2/httpd.conf are in reference to Directory "/Library/WebServer/share/httpd/manual" and Directory "/usr/share/httpd/error" - they are both set to None.

Mar 28, 2008 10:20 PM in response to Scott Joing

Hi Scott,

I recommend not doing major edits to the config files in the /etc/apache2/sites directory. These are sometimes 'auto-cleaned' of custom edits by Server Admin when editing and committing through it (very annoying). Here's what I did that works well for me and has not been auto-cleaned so far:

1) Make a dir /etc/apache2/users

2) Make new files in that dir that have names based on your server's web users, e.g. myuser.conf (should not be system users, and not have their web directories in the /Library/Webserver area, IMHO).

3) Add *Include "/etc/apache2/users/myuser.conf"* to the end of and inside of the <VirtualHost> directive in your site config file in question from /etc/apache2/sites dir.

4) Add all your custom edits to the user file you need, in this case the 301 redirect (see below for my favorite add-www-301 redirect). If using BBEdit, make sure to re-lock the files when done, but you can leave them open.

5) In Terminal run +apachectl -t+ to make sure you have no syntax errors in your config edited files, or the Apache will crash when you try to restart it, taking your sites off-line.

5) Open /var/log/apache2/error_log, with +tail -F -n 50 /var/log/apache2/error_log+ or in Console on the server.

5) Then, in Server Admin under Web -> Sites, make a trivial change and reverse it (like checking then un-checking a box) as this allows you to gracefully restart Apache without screwing up the Webcache feature (see Web PDF docs about not using +apachectl graceful+ ).

6) Go back to your Apache error log and make sure Apache did not crash. If it did, reverse your edits and restart Apache again.

Here is my favorite add-www-301 redirect:

<IfModule mod_rewrite.c>
RewriteEngine On
# Google canonical redirect
RewriteCond %{HTTP_HOST} ^\[^\.\]+\.....?$ \[NC\]
RewriteCond %{HTTPS} !^on$ \[NC\]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 \[R=301,L\]
RewriteCond %{HTTP_HOST} ^\\[^\.\\]+\.....?$ \[NC\]
RewriteCond %{HTTPS} ^on$ \[NC\]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 \[R=301,L\]
</IfModule>

As opposed to other redirects that state the name of the domain, this will cover sites that have multiple domains assigned to them and up to four letters in the TLD (like .info). Also, it handles https redirects as well. Always adding (or removing) the www is important to keep your sites page ranking in Google from being diluted by the non www use (or use) of the base domain. You may already know this, of course.

The new book http://buildingfindablewebsites.com/ explains this well, though its mod_rewrite solution in limited. Caveat: HTTPS part may not work well with older browsers that do not support HTTP 1.1.

Larry

Mar 30, 2008 4:15 PM in response to Scott Joing

Scott,

Glad to hear it works for you. It works for my https setup, but I get a notification that the certificate is mismatched (before the rewrite), which is true since our certificate is specifically set to www.mydomain.com. This rewrite should work well if you have a wildcard certificate set up for your base domain.

I have reasearched this a little more and come up with:

<IfModule mod_rewrite.c>
# Google canonical
RewriteRule ^ - \[E=via:http\]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^ - \[E=via:https\]
RewriteCond %{HTTP_HOST} ^\[^\.\]+\.....?$ \[NC\]
RewriteRule ^(.*)$ %{ENV:via}://www.%{HTTP_HOST}$1 \[R=301,L\]
</IfModule>

It is really just the same, stated differently and a little less redundant. It probably will not work for your https if you do not have an https virtual domain set up yet in Apache. Here's the breakdown:

RewriteRule ^ - \[E=via:http\]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^ - \[E=via:https\]

This sets an environment variable named "via" to "http," then checks for use of the https port (which defaults to 443). If found, it sets "via" to "https" instead. I used SERVER_PORT instead of HTTPS as this will be more compatible with older Apache setups.

RewriteCond %{HTTP_HOST} ^\[^\.\]+\.....?$ \[NC\]
RewriteRule ^(.*)$ %{ENV:via}://www.%{HTTP_HOST}$1 \[R=301,L\]

Condition is the same as before (works with web server aliases, like other mirrored domains), but now the rewrite includes the previously assigned variable.

Larry

301 redirect - best way?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.