0

I followed the instructions in the comments of the .htaccess file located in the root of my Drupal 6.22 web site (lines 93-94 below).

This configuration does not work. Any idea where I might be going wrong?:

#
# Apache/PHP/Drupal settings:
#

# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.
<Files favicon.ico>
  # There is no end quote below, for compatibility with Apache 1.3.
  ErrorDocument 404 "The requested file favicon.ico was not found.
</Files>

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 4, Apache 1.
<IfModule mod_php4.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 4, Apache 2.
<IfModule sapi_apache2.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php$>
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior. This may
    # fail if an error occurs early in the bootstrap process, and it may cause
    # problems if a non-Drupal PHP file is installed in a subdirectory.
    ExpiresActive Off
  </FilesMatch>
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  RewriteCond %{HTTP_HOST} ^example\.ac\.uk$ [NC]
  RewriteRule ^(.*)$ http://www.example.ac.uk/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

# $Id$

Thank you.

unpossible
  • 218
  • 3
  • 9
  • Covers much the same ground as ["How to redirect non-www to www without hardcoding using .htaccess?"](http://serverfault.com/questions/190589/). – JdeBP Jun 29 '11 at 10:50

2 Answers2

1

Looks like you forgot to escape second . character. It should be:

RewriteCond %{HTTP_HOST} ^example\.ac\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.ac.uk/$1 [L,R=301]

On another hand -- . represents ANY character, so should work just fine.

In any case -- this is easier approach as no regex is involved:

RewriteCond %{HTTP_HOST} =example.ac.uk [NC]
RewriteRule ^(.*)$ http://www.example.ac.uk/$1 [L,R=301]
LazyOne
  • 3,014
  • 1
  • 16
  • 15
  • Thank you also LazyOne, but this doesn't work either. – unpossible Jun 28 '11 at 14:30
  • OK, few ideas here. Let's try it one by one. For testing purposes put these rules on the very top and make `RewriteEngine on` your very first line. Maybe you behind some sort of proxy .. or PHP is run somehow differently (don't know), but lets try this: **1)** Remove `$` character so the rewrite looks like this: `RewriteCond %{HTTP_HOST} ^example\.ac\.uk [NC]` and `RewriteRule ^(.*)$ http://www.example.ac.uk/$1 [L,R=301]` -- this should katch situations when you also have port number as part of HTTP_HOST. – LazyOne Jun 28 '11 at 17:55
  • **2)** Please use a Firebug for Firefox, "Net" panel or any other similar tool) to confirm that 301 redirect actually happens (or not). **3)** let's do other way around -- and rewrite all NON www.example.ac.uk to this domain: `RewriteCond %{HTTP_HOST} !^www\.example\.ac\.uk$ [NC]` `RewriteRule ^(.*)$ http://www.example.ac.uk/$1 [L,R=301]` – LazyOne Jun 28 '11 at 17:56
  • **4)** Create simple php file `echo.php` and put it into root folder: `` Add this rule just below RewriteEngine on: `RewriteCond %{REQUEST_URI} !^/echo\.php` `RewriteRule (.*) /echo.php?host=%{HTTP_HOST}&url=$1 [L]`. Please comment out ALL other rewrite rules (preferably all directives altogether). Now have a look at the variables dump, especially at QUERY_STRING – LazyOne Jun 28 '11 at 18:01
  • The redirect works when accessing the site from home, which suggests that the network configuration at work is to blame. Internet traffic may well be routed through a proxy. Why would this configuration prevent the redirect from working? With hindsight, I should have tried to access the web site from my smartphone or something first. Lives and learns! Thanks for all your help LazyOne. – unpossible Jun 28 '11 at 20:53
0

might be missing another \

RewriteCond %{HTTP_HOST} ^example\.ac.uk$ [NC]

should be

RewriteCond %{HTTP_HOST} ^example\.ac\.uk$ [NC]
Mike
  • 21,910
  • 7
  • 55
  • 79