Htaccess redirect

software development

#1

I want change my site so if someone goes to http://mydomain.com they are redirected to http://www.mydomain.com - and it would be nice if this was true of any page on the site.

I STF’d and found this to go from http://www.domain.tld to http://domain.tld

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

Would this be the correct way to do the reverse?

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

Thanks.


#2

It looks ok.


#3

well, I tried it and it didn’t work.

anyone have an idea how to do it?

Thanks.


#4

Copied directly from the Apache config from a site I’m doing this same thing on (domain name changed, obviously):

RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.org [NC] RewriteRule ^/(.*) http://www.example.org/$1 [L,R] As this is almost exactly what you have, the problem might be your lack of ‘RewriteEngine On’. Without that, mod_rewrite is not enabled.


#5

hmmm, thanks. still didn’t work.


#6

At the time of my earlier reply, I hadn’t yet done it here and just posted the mod_rewrite rules I use on other web servers. These rules do work.

HOWEVER, having started moving a couple sites over to DH, I find that these same rewrite rules do not work here. For the record, the following is exactly how they appear both here and on the old web server, where they work fine:

RewriteEngine On RewriteCond %{HTTP_HOST} ^softblackstars\.org [NC] RewriteRule ^/(.*) http://www.softblackstars.org/$1 [L,R] The only difference is that here they are in .htaccess in the root directory of the site and on the old server they were in the Apache config file itself (I administered the machine myself and had the luxery of modifying the main server configuration). I have verified that no rewrite rules are working; the problem is not isolated to the HTTP_HOST example above. The rules tested all work fine on other systems. Adding RewriteBase (required for some per-directory rewrite rules) does not help.

DH support, while being extremely helpful otherwise, was unable to offer any suggestions on what the problem might be. This is a bit of a problem for me, as I’m currently developing a site that relies heavily on mod_rewrite. Not finding an answer to this problem will prevent the site going live without major modification (that I’m not particularly willing to do).


#7

How about:

RewriteCond %{HTTP_HOST} !^www.softblackstars.org [NC] RewriteRule ^(.*)$ http://www.softblackstars.org%{REQUEST_URI} [R=301] I have it on my site going the other way around…I want to get rid of the “www.”

Mine look like this:

RewriteCond %{HTTP_HOST} !^mydomain.net [NC] RewriteRule ^(.*)$ http://mydomain.net%{REQUEST_URI} [R=301] blueorder


#8

The syntax is fine, the problem is that rewrite rules are not running at all. Even simple rules like this are not working:

RewriteRule ^/foo/(.*) /bar/$1 I’ve spent a good amount of time today banging my head against this one and have narrowed it down to a possible problem with using mod_rewrite with .htaccess, as opposed to putting the rules in the main server or virtual host configuration. The docs recommend using the RewriteBase directive for per-directory (eg, .htaccess) rules, but that doesn’t seem to help either.

Can someone post an example of some rules that are currently working on their DH site?


#9

Here is an excerpt of my .htaccess:

[code]DirectoryIndex index.php

ErrorDocument 404 /404/

AddDefaultCharset utf-8
AddType application/download .klip

RewriteEngine On
RewriteCond %{HTTP_HOST} !^theamells.net [NC]
RewriteRule ^(.*)$ http://theamells.net%{REQUEST_URI} [R=301]
RewriteRule ^klip/?$ /wp_klip.php [QSA,L]
RewriteRule robots.txt$ robots.php [L,R=permanent,NC]
RewriteRule ^mobile/?$ /wp-mobile.php [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .php$ /404/
[/code]blueorder


#10

Thank you blueorder. I found the problem.

There seems to be a subtle difference between how mod_rewrite processes rules depending on where the rules are, in an Apache config file or .htaccess.

Note the difference between your HTTP_HOST rewriterule and mine (aside from the fact that we’re looking for the opposite result). My rewrite rule began

RewriteRule ^/(.*) while yours begins:

RewriteRule ^(.*) That leading slash was responsible for my rule not working. The slash is fine if the rule appears in a virtualhost container in an Apache config file, but not if it’s in a per-directory (eg, .htaccess) rule. It works without a slash in either location, of course, since the regex (.*) will match a slash as well as any other character.

I have a headache now.


#11

hehehehe…one slash…

Good that you found the a solution to the problem and am here to help…

blueorder