Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite redirect example.com to www.example.com

mod_rewrite redirect

         

jpl80

6:05 pm on Dec 8, 2008 (gmt 0)

10+ Year Member



I need every page in my site to have the www prefix or else my SSL certificate will not work. My hosting provider had me try this .htaccess file in my root directory:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

But that is not working. That was putting "/cgi-bin/php5-cgi/" in my directory and returning a 500: Internal Server Error.

http://www.example.com/cgi-bin/php5-cgi/drupal-5.10/index.php?q=beerpongtournaments

[edited by: jdMorgan at 8:21 pm (utc) on Dec. 8, 2008]
[edit reason] Use example.com only, please. [/edit]

g1smd

8:50 pm on Dec 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is likely that you have rewrites before this redirect.

Place this redirect before those rewrites and it might play nicer.

It still might fail, but that will be a second problem on top of the first one.

jpl80

9:43 pm on Dec 8, 2008 (gmt 0)

10+ Year Member



no rewrites above in this my first .htaccess folder but there is another in my drupal subfolder:

www.example.com/.htaccess
www.example.com/drupal-5.10/.htaccess

which .htaccess gets precedence?


--------------------------------------------------
This is the .htaccess inside the drupal folder:
--------------------------------------------------

<FilesMatch "\.(engine¦inc¦info¦install¦module¦profile¦po¦sh¦.*sql¦theme¦tpl(\.php)?¦xtmpl)$¦^(code-style\.pl¦Entries.*¦Repository¦Root¦Tag¦Template)$">
Order allow,deny
</FilesMatch>

Options -Indexes

Options +FollowSymLinks

ErrorDocument 404 /index.php

DirectoryIndex index.php

<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>

<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>

<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>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A1209600
ExpiresByType text/html A1
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

# $Id: .htaccess,v 1.81.2.4 2008/01/22 09:01:39 drumm Exp $

jdMorgan

3:51 pm on Dec 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must add an [L] flag to your redirect, so that it will be invoked immediately rather than allowing subsequent rules (in this .htaccess file and others below it in your directory structure) to execute before the redirect actually takes place.

/drupal-5.10/.htaccess takes precedence for URLs starting with /drupal-5.10

If RewriteOptions Inherit is set to "on", then /.htaccess will initially take precedence in the sense of executing first, but /drupal-5.10/.htaccess will be able to override some configuration settings if the resulting URL-path after /.htaccess execution still starts with /drupal-5.10

If RewriteOptions Inherit is set to "off", then /.htaccess will not execute for requested URL-paths starting with /drupal-5.10

*Some* agent is prepending the /cgi-bin/php5-cgi/drupal-5.10 filepath to your URL-path before your redirect is invoked. This could be mod_rewrite, mod_alias, mod_dir, or mod_negotiation -- in either your .htaccess files or in a server config file, or it could be one or more of your scripts. The problem could also be caused by a server-level misconfiguration. If adding the [L] flag doesn't help and you cannot find the cause, then lean on your host for more support.

Jim

jpl80

2:12 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



got everything working by moving my drupal installation to the root directory.

Now my question is how do I redirect my users who have bookmarks or click on entries in the search engines?

I want to redirect them from:

http://www.example.com/durpal/ (with or without 'www')

To:

http://www.example.com

Would I place a rule before or after the 'www' rewriteRule? I have requested that Google take my results out for now because I don't want to keep getting a 404 error as that could hurt my rankings? What hurts my rankings more?

jdMorgan

5:57 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because a redirect for example.com/drupal is more-specific (e.g. affects fewer URLs) than a redirect for example.com/*, the example.com/drupal redirect should be placed before the "catch-all" domain redirect.

A good rule of thumb is this: All external redirects go first, in order from most-specific to least-specific, followed by all internal rewrites, again in order from most-specific to least-specific. Where two rules' RewriteRule and RewriteCond patterns are mutually-exclusive, the order won't matter.

Jim

jpl80

6:33 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



So it be:

RewriteCond %{HTTP_HOST} ^www\.example\.com/drupal\-5\.10$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

OR something like this?

RewriteRule ^/drupal-5.10$ /

I'm not clear on this. Also, I've deleted the drupal directory so putting an htaccess in there isn't an option unless I recreate it.

g1smd

8:19 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not quite. Use your first example.

In HTTP_HOST state only the domain name (not any folders), but use

(www\.)?
to make the www part optional.

In the second line prepend

(.*)
with the
drupal-5\.10/
filepath.

jpl80

9:07 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Like This?

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/(.*)drupal-5\.10/$1 [L,R=301]

Just to clarify, I want the drupal-5.10 subpath dropped out completely because the directory has been removed and all my files are now in the root.

g1smd

9:15 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Drop the $ off the first line.

On the second line, I have no idea how you got to that. Prepend means "add to the start" (as opposed to append which is "stick on the end").

jdMorgan

9:52 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^drupal-5\.10/(.*)$ http://www.example.com/$1 [R=301,L]

Jim