Welcome to WebmasterWorld Guest from 54.197.116.116

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite - not working

   
3:16 pm on Sep 28, 2004 (gmt 0)

10+ Year Member



Im new to apache, but am trying to set setup 301 pernament redirects.

eg www.widgets.com
and www.red-widgets.com
both redirect to www.widgets.co.uk

I used the following code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^widgets.com$ [NC]
RewriteCond %{HTTP_HOST} ^red-widgets.com$ [NC]
RewriteRule ^(.*)$ [widgets.co.uk...] [R,L]

when run, the site just says "forbidden"

Is the above code correct?
Without asking my hosts, is there a way to see which modules/directives are running on the server? (code to detect them?)

thanks

3:36 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Andrew,

I'm assuming you've placed the code in .htaccess in your Web root directory. If so, try the following:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^widgets\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?red-widgets\.com [NC]
RewriteRule ^(.*)$ http://www.widgets.co.uk/$1 [R=301,L]

Options +FollowSymLinks or Options +SymLinkIfOwnerMatch is required in order to use mod_rewrite. The domain names should not be end-anchored in order to prevent rule failure in case a port number is appended, e.g. "widgets.com:80/index.html".

This is a relatively simple rewrite, so if it does not work, then it's likely that mod_rewrite is not enabled on your server -- Check your server error log to see what it says.

Jim

4:22 pm on Sep 28, 2004 (gmt 0)

10+ Year Member



thanks, that worked fine
8:21 am on Oct 6, 2004 (gmt 0)

10+ Year Member



I understand most of this,

RewriteCond %{HTTP_HOST} ^widgets\.com [NC,OR]

but why is the \ before the .com

thanks

12:45 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> but why is the \ before the .com

Because the period has special meaning to the regular-expressions pattern-matching language used in mod_rewrite. Without a preceding the slash, a period in a pattern means "match any character." Any character that has special meaning in regular expressions must be escaped if used inside a pattern.

In this case, it is used to specify that you want a literal period to match, and not just any character.

More info is available in the regular-expressions tutorial cited in our charter.

Jim