Apache redirect domain without www to the same with www except one page - apache

I'm new in apache redirect.
I want to redirect example.org (without www) to www.example.org except one page
so I did this :
<VirtualHost *:8001>
ServerName example.org
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.org$
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
</VirtualHost>
and it doesn't work
Have any idea ?
thank you

Use this in your .htaccess instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^\/(example)
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
The first condition checks to see if www is not present. The second condition makes sure that it is not the directory example. If both of these conditions are met, then it will force www.
Make sure you clear your cache before testing this.
EDIT:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Related

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

All http traffic to domain.com and all https to sub.domain.com with htaccess

I have two domain names www.domain.com and sub.domain.com, and I want to redirect all the http traffic to www.domain.com and all the https traffic to sub.domain.com. For example, if the url called is http:// sub.domain.com/index.php, the server have to rewrite the url to http:// www.domain.com/index.php, and if the url called is https:// www.domain.com/index.php, the server have to rewrite the url to https:// sub.domain.com.
I have this code in htaccess but it does not work correctly:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/sub\.domain\.com\/" [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
It redirects correctly the https to sub.domain.com but only when there is no page indicated (https:// domain.com/index.php/some-page not work and https:// domain.com yes). For the http not work at all.
Thanks in advance!
You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Make sure both domain.com and sub.domain.com share the same document root folder. Otherwise, you will have to split htaccess code in two htaccess (one per root domain folder)
/domain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
/subdomain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

https htaccess exclude some url

I know this question has been asked a lot, but i try a lot of answers and didnt succeed.
i try those answers:
.htaccess ssl & non-ssl redirects
Remove SSL integration from a specific folder using htaccess
and a few others that i find on google.
i only have ssl on 3 pages of the site and i want to make a general rule, that the other pages should be redirect to http when they are on https. im like Jon Snow i know nothing about apache and htaccess.
so this what i try so far:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^\/(page1|page2|page3)+ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
and this:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/page1/$
RewriteCond %{REQUEST_URI} !^/page2/$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
the part of send to http works but the exception dont or they send all to http or redirect to the index.
to clarify i didnt put both codes at same time.
other rules on:
RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L]
RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php
full url as requested:
https://www.example.com.br/area-restrita/
it can have more information after the 'area-restrita' part
Try this code:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(area-restrita|page2) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L,NE]
RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php [L,NC]
if you want to redirect the whole site to https except for some particular directories / URLs and in these particular cases you still want them served via http, you can use this technique in virtual.conf; otherwise when the RewriteCond match, the particular pages will always receive the error "404 page not found"
<VirtualHost *:80>
ServerName www.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/app1/.*$ [NC]
RewriteCond %{REQUEST_URI} !^/app2/.*$ [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !=apps.example.com [NC]
RewriteCond %{REQUEST_URI} ^/app1/.*$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app2/.*$ [NC]
RewriteRule ^.*$ http://apps.example.com%{REQUEST_URI} [R,L,P]
</VirtualHost>
<VirtualHost *:80>
ServerName apps.example.com
DocumentRoot /home/sites/example.com/public_html
</VirtualHost>

.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

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.