rewrite www. to without www. in .htaccess for hostname instead of writing in the website name? - apache

I currently use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.e-innovate.co.uk [NC]
RewriteRule ^(.*)$ http://e-innovate.co.uk/$1 [L,R=301,NC]
But what i am trying to do is create a general rewrite code which i can drop into .htaccess files without having to change the domain name such as below, i dont seem to be able to get this to work, am i on the right track ?
Thanks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
note that this will also get applied to subdomains.

Related

301 Redirect all ?p=xxx to frontpage

We just got our new site up and running. But now I'm trying to sort out the redirects with .htaccess.
Most of them work, but the once with /?p=xxxx wont redirect to mysite.com/something/ now I don't know what all the ?p=xxxx pages are so I just want them to redirect to the frontpage.
Anyone know how to do that. I have seen a lot of these codes. But non of them work for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If anyone can help, thanks!
You can use the following :
RewriteEngine On
##non-www to www##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
##redirect "\?p=xxx" to "/frontpage"##
RewriteCond %{THE_REQUEST} \?p=[^\s]+ [NC]
RewriteRule ^ /frontpage/? [L,R]

.htaccess Exclude directory in rewrite

I want to exclude URL rewriting if the directory is mysite.com/admin. I have tried:
# prepend http://www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(admin|user)($|/) - [L]
I'm not too familiar with this stuff, but I can't seem to get this working. Does anyone see the solution?
Try moving the pass-through for the admin | user directories before the redirect:
RewriteEngine On
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Other than that, I don't see any rewriting going on at all, so I'm not sure what they need to be excluded from.

Forcing WWW with httpd.conf rather than .htaccess

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]

.htaccess redirection - two sharing websites

I have two websites that is actually the same where example.com shares all files from examples.com. So whatever changes made in exampples.com, example.com automatically gets updated. That means they have the same .htaccess file.
The problem is, I want to both sites redirects to non www to a www url. I got this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This should do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^examples\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Basically you're adding an OR condition to say if either example.com or examples.com doesn't begin with www. then add it to the respective domain name.
replace example.com with %{HTTP_HOST} to make your rules host independent

.htaccess rewrite and subdomains

I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:
php_flag display_errors off
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:
RewriteRule ^(.+) /online_content/$1 [L]
I get an error
I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.
And yes, I do need to rewrite to a directory that has a different name than the subdomain.
Well, I figured it out.
The issue was that I was causing an infinite loop.
Once the rewrite had happened, it was still trying to rewrite to the directory.
Here is my new htaccess that took care of it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]
Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.
Try this rule:
RewriteCond %{HTTP_HOST} =onlinedev.example.com [NC]
RewriteCond $0 !^online_content/
RewriteRule .+ /online_content/$0 [L]