I have several web sites and using the same mod_rewrite. I like to add two additional conditions to it and based on many of solutions that I have found on here and none seem to be working.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have my sites working with the following solution.
domain.com forwards to www.domain.com
I like to add the following two conditions to my conditions above.
Remove trailing slashes. If I have www.domain.com/ it does remove
that trailing slash, unfortunately, for some reason if there are
multiple slashes at the end it doesn't www.domain.com//
Removing index.html from www.domain.com/index.html
Making this dynamic is what may be through this off. If I use the suggestion on domain itself, I have not problems. It is once I add http://www.%{HTTP_HOST}%{REQUEST_URI} where everything goes wonky.
i use this rewrite for redirecting domain.com to www.domain.com. No problems with //
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R,L]
#this could work for you (untested)
RewriteCond %{HTTP_HOST} !^www.\$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R,L]
#redirect /index.html to /
RewriteRule ^/index.html / [R=301,L]
Related
I'm trying to redirect my website about dentists in London from the non-www to the www version and I'm using the following code on the .htaccess of my server, but it doesn't work. What do I miss?
Thank you
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.topdentists.co.uk [nc]
RewriteRule ^(.*)$ http://www.topdentists.co.uk/$1 [r=301,nc]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This is more general and will work for any site, not just the one you are currently working on. Taken from: Dense13.com (explanation of the code is in the second comment, underneath the article)
The reason why I think your code didn't work is because the RewriteCond uses a regular expression, but you didn't escape the dot (which is a regex-character).
To properly handle any potential https requests, as well as any strange characters, something like this is usually recommended:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]
I have a domain name www.domainA.com I want to redirect it to domainB in following manner.
www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand
How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.
RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]
URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]
Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
You can get more info from webmaster world forum
I've read up on many Force WWW. tricks using htaccess. (As Below)
Rewritecond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) http://www.domina.com/$1 [R=301,L]
I'm wondering if there is a method to force WWW using httpd.config across the entire site.
The htaccess works a treat, but I have many subdirectories, most of them include there own .htacess files, which breaks the force www in that particular subdirectory.
So I was thinking, is there a way to force this "www." into httpd.config, across the entire site without editing .htaccess?
Thanks.
This should do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And to maintain HTTPS too:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works for me
# direct all variation of FQDN to www.epoch.co.il
RewriteEngine on
#RewriteLogLevel 3
#RewriteLog "/var/log/httpd/epoch/www/epoch-rewrite_log"
RewriteCond %{HTTP_HOST} !^www\.epoch\.co\.il [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.epoch.co.il/$1 [L,R]
I have the problem but with many subdomains: E.g..
sub1.domain.com and new.domain.com and xsub.domain.com and many many more like this.
How do I remove the www from in front of any of these with one generic rule.
E.g. if someone types www..domain.com or http://www..domain.com to change it to
http://.domain.com
Thanks
You may use the rewrite module to remove the www. when it precedes a sub-domain. In this way, an address like: www.sub1.domain.com would be redirected to sub1.domain.com:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
</IfModule>
Modified tested solution but for http only.
#Allow domain of the form www.domain.com
RewriteCond %{HTTP_HOST} !^www\.([^\..]*)\.([^\..]*)$ [NC]
#Otherwise any other form must be rewritten to remove www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
#Substitue the complete domain using group %1 in the parentheses of the above condition
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
This will do the job, and return with http://
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]
For https, just add the s, as so:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) https://%1/$1 [R,L]
Note that this can be done in httpd.conf as well. Using .htaccess is popular but actually slows down the site, especially if there are nested directories.
You do need:
Options +FollowSymLinks
But you do not need Indexes.