Mod rewrite half working

What I have
-I have a domain that is my main domain mywebsitecompany.com.
-I also own a shortened domain mwcompany.com that I want to rewrite to the above url…
-I also want all redirects/rewrites to point to the “www” version of the domain (www.mywebsitecompany.com)

Issues in red
[COLOR=“Green”]- mywebsitecompany –> www.mywebsitecompany.com

this works

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

# this needs help
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mwcompany\\.com
RewriteRule (.*) http://www.mywebsitecompany.com/$1 [R=301,L]

why isnt “www.mwcompany.com” rewriting to “www.mywebsitecompany.com

Because it matches neither of your HTTP_HOST conditions.

Here’s a rule that will force any URL that’s not www.mywebsitecompany.com to redirect to www.mywebsitecompany.com. Then you don’t need a separate rule for each domain and subdomain.

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

Dan,

May I revise your mod_rewrite slightly?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www[COLOR="Red"]\\[/COLOR].mywebsitecompany[COLOR="Red"]\\[/COLOR].com$ [COLOR="Red"][NC][/COLOR]
RewriteRule [COLOR="Red"][COLOR="Gray"]^(.*)$[/COLOR][/COLOR][COLOR="Red"].?[/COLOR] http://www.mywebsitecompany.com[COLOR="Gray"]/$1[/COLOR][COLOR="Red"]%{REQUEST_URI}[/COLOR] [R=301[COLOR="Red"],L[/COLOR]]

REMOVE the gray code and ADD the red code.

What I did was to:

  1. add the escape character before the dots (to force them to match the dot character rather than any character)
  2. add the No Case flag because the %{HTTP_HOST} is case INsensitive
  3. replaced the (.*) (it’s not needed, it’s already available as the %{REQUEST_URI} variable) and
  4. add the Last flag.

Whew, okay, not quite slightly but these small points are pedantically correct.

Regards,

DK

David, I used your modified version and it broke the site… here it is

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.mywebsitecompany\\.com$ [NC]
RewriteRule .? http://www.mywebsitecompany.com/%{REQUEST_URI} [R=301, L]

Dan, your code worked but I have a small issue when adding a directory to the URL, if i dont use the “www” version it breaks:
mywebsitecompany.com/admin –> error
www.mywebsitecompany.com/admin –> works

Dan, any idea why when directly linking to a directory like “mywebsitecompany.com/admin” breaks but linking to “www.mywebsitecompany.com/admin” works?

rip,

REMOVE the / before %{REQUEST_URI} in the redirection.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.mywebsitecompany\\.com$ [NC]
RewriteRule .? http://www.mywebsitecompany.com%{REQUEST_URI} [R=301, L]

Regards,

DK

DK, thank you for the followup! I tried it again as you posted and it breaks again

rip,

Hmmm, NOTHING there to break mod_rewrite. Please show your entire .htaccess file’s contents.

Regards,

DK

Sure, thanks!

<Files .htaccess>

order allow,deny

deny from all

</Files>



Options -Indexes

ErrorDocument 404 /error.php


RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\\.mywebsitecompany\\.com$ [NC]

RewriteRule .? http://www.mywebsitecompany.com%{REQUEST_URI} [R=301, L]


redirect 301 /press.php http://www.mywebsitecompany.com/press-media.php

redirect 301 /purchase1.php http://www.mywebsitecompany.com/page4?id=EMT-NB

redirect 301 /page1.php http://www.mywebsitecompany.com/page3.php?id=all

redirect 301 /page2.php http://www.mywebsitecompany.com/page3.php?id=all

rip,

I believe that Apache chokes on extra spaces in the mod_rewrite, i.e., [R=301, L] should be [R=301,L]. Otherwise, fine.

Regards,

DK

Thanks David, yes that space was causing the error.

The only issue right now is with directories. The only url that works is when the full “www” version of the main URL is entered. non-www versions or forwarding addresses give a 404 error. Is this an .htaccess issue?

www.mycompanywebsite.com/admin –> works
mycompanywebsite.com/admin –> 404 error
www.mcwebsite.com/admin –> 404 error
mcwebsite.com/admin –> 404 error

rip,

Yes.

.

.

.

When you use

RewriteCond &#37;{HTTP_HOST} !^www\\.mywebsitecompany\\.com$ [NC]

you are requiring the {HTTP_HOST} to match the www subdirectory. If that’s to be optional, use

RewriteCond %{HTTP_HOST} !^(www\\.)?mywebsitecompany\\.com$ [NC]

BTW, I don’t see the code you’re using to test a subdirectory so I can’t be sure what you’re asking about (or the reason for the failure to match).

Regards,

DK