Permanent redirection through apache rewrite rules

I'm trying to write a rule to permanently redirect a domain name to another domain name

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

This only works if the user remembers to enter www, if the user does not enter www in the URL, the page loads, but the links to the image are broken.

Does anyone know how to configure the above rule to work with and without www?

I am using LAMP configuration, apache 2 on redhat.

+5
source share
3 answers

You can provide some additional rewriting conditions with [OR]:

RewriteCond %{HTTP_HOST} ^www.companyname1.com$ [OR]
RewriteCond %{HTTP_HOST} ^companyname1.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]

. Rewrite-Condition , WWW, , www .

+9

, , - , @Demento.

# Parmenent redirect to webdesign.danols.com of all pages
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.kingston-web-design.com [OR]
RewriteCond %{HTTP_HOST} ^kingston-web-design.com
RewriteRule ^(.*)$ http://webdesign.danols.com.com$1 [R=301,L]
+2

, , , , companyname1.com.

, .

RewriteCond %{HTTP_HOST} companyname1.com$
RewriteRule ^/?(.*) http://www.companyname2.com/$1 [R=permanent,L]
+1

All Articles