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]
dklynn
January 21, 2010, 1:45am
3
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:
add the escape character before the dots (to force them to match the dot character rather than any character)
add the No Case flag because the %{HTTP_HOST} is case INsensitive
replaced the (.*) (it’s not needed, it’s already available as the %{REQUEST_URI} variable) and
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?
dklynn
January 21, 2010, 9:21pm
6
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
dklynn:
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
dklynn
January 22, 2010, 4:22am
8
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
dklynn
January 23, 2010, 10:01am
10
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
dklynn
January 26, 2010, 9:42pm
12
rip,
Yes.
.
.
.
When you use
RewriteCond %{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