.htaccess rewrite - http:// to http://www - .htaccess

I am new to .htaccess redirection.
I am using the below code
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
But when i hit the URL http://example.com/folder/file.php i'm getting redirected to http://www.example.com/file.php which is wrong.
Can someone please help me with this
Help is greatly apreciated
Thanks

Alright can you try this in your .htaccess or apache conf file:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

There are some typos and some unescaped characters, but most important you're missing a needed "$" character in that *rewritecond %{http_host} ^example.com [nc]* line.
Here is your code "as it should have been":
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
Though, I personally use and would recommend doing it like this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^!www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
That should get you going... ;)

Related

How to redir subfolders to different domain than root domain

IHi, I'm to this, I've read and tried all tips I've found here, but it's still not working the way I need.
I'm using htaccess to redir domain and it's structure to new domain:
`rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
It redirs all subfolders e.g. www.olddomain.com/contact/ to www.newdomain.com/contact/ etc., so far so good, BUT I need to redir the mainpage, the one page only, to different URL:
from www.olddomain.com to www.newdomain.com/about-old/
All I've tried this:
`Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/about-old/ [r=301,nc,L]
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
but it just redirs EVERYTHING to www.newdomain.com/about-old/
Any ideas, please?
Thanks you
To match home page pattern you need is:
^/?$
So your full code will be:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^/?$ http://www.newdomain.com/about-old/ [R=301,L]
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^(.+)$ http://www.newdomain.com/$1 [R=301,L,NE]

Very easy htaccess redirect works in the opposite way

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
this is not working, while this is working:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
with the negation in front of the condition. I've read a lot of material about .htaccess in last hour, but I can't realize yet what am i doing wrong...
%{HTTP_HOST} contains domain name ONLY, e.g. example.com, www.example.com etc and does NOT include protocol. Therefore:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^agilerent\.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]

How to use "NOT" operator on .htaccess redirection?

I'm using .htaccess to redirect users from multiple old domains to a new domain. Something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite1.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite2.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite3.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
However, I'd like to know if I could use the NOT operator to redirect any domain except newsite.com, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond != %{HTTP_HOST} http://newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
Thank you very much!
Official manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
You have placed != in wrong place PLUS %{HTTP_HOST} contains domain name only so no protocol should be there:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]

htaccess remove www from URL respect RewriteBase

In base directory it works fine, but if you go in subdirectory: example www.domain.com/dir/
used RewriteBase is lost.
In htaccess I have something like this....
Options +FollowSymLinks
RewriteEngine On
RewriteBase /dir/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteRule ^home/?$ index.php [L]
So if we put www.domain.com/dir/home it redirect us to http://domain.com/home and /dir/ is missing ...
What I is wrong ... Thanks for ideas.
Just put dir in Rule
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/dir/$1 [L,R=301]

.htaccess redirect subdomain to main site - no exceptions

I'm trying to redirect all requests for subdomain.example.com to www.example.com
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Seems to cause a 500 internal server error as does
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
So my question is why are these failing and how do I fix it?
After your first two lines, add:
RewriteCond %{http_host} ^.domain.com
RewriteRule ^(.*) http://domain.com/$1 [R=301]

Resources