Redirect index page to different file then the rest of the redirects using htaccess - apache

Not sure how to pull this off. Say I have example.com and example2.com. I want all requests from example2.com to 301 redirect to example.com. That part isn't hard.
RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
What I would like to do though when someone enters www.example2.com is to have them redirected to www.example.com/a-special-page.html and have everything else redirect exactly as entered. ie www.example2.com/another-page.html redirects to www.example.com/another-page.html

Have specific redirect first and then the generic redirect like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com$ [NC]
RewriteRule ^/?$ http://www.example.com/a-special-page.html [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Make sure to test it after clearing your browser cache.

Related

htaccess http to https with www. Without redirecting sub domain

I have a RewriteRule that redirects my main domain to https://www.sta-games.com which works fine, but when I try to access my subdomain http://files.sta-games.com, it redirects to my main domain.
Heres my redirect rules
#HTTPS Redirection
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone see the problem?
You need to adjust your rules so that it looks for the whole domain instead of just part of it. Right now you are only looking for the absence of www. So that is why your subdomain redirects.
First you need to combine your rules because it seems you want to redirect if https is not on and there is no www, so make that one rule. Then use your actual domain name in the rule. Replace example.com with your domain. This should fix your issue.
#HTTPS Redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
Have an additional skip condition for all domains except the main domain:
RewriteCond %{HTTP_HOST} ^(www\.)?sta-games\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.sta-games.com%{REQUEST_URI} [L,R=301,NE]
Test this after clearing your browser cache.

.htaccess Redirection [force www except index.php]

I'm trying to do an htaccess redirection based on this condition :
Redirect all pages without "www" http://siteweb.com to http://www.siteweb.com/index.html
Except http://siteweb.com/index.php
The http://siteweb.com/index.php must to be redirected to http://www.siteweb.com/index.php
Actually i use this code [but something wrong on it :s]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^/index.php$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks in advance.
You can use this code in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (?!^index\.php$)^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]
This will redirect all URIs except index.php to a domain with www. However your question is confusing because you also wrote that:
The http://siteweb.com/index.php must to be redirected To http://www.siteweb.com/index.php
Which tells me that may be you want to redirect every URI including index.php to a domain with www.

htaccess: exluce some domain in RewriteCond

this is my .htaccess code
so if the user type just domain.com will be redirected to www.domain.com
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
my problem now is that i have a new domain pointing to the same domain path
so even the new domain is redirected "transparently" to domain.com...
how can i exclude some domain name from that rule?
thanks!
You could try making the rewrite generic, so all requests that are not starting with www are redirected, but on the correct/requested domain.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
Or, you can check instead for if the domain starts with domain.com:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
Hopefully this helps.

.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

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client