Canonical 301 Redirect for Apache - apache

If I have these dummy domain names example1.com and example1.net available. I want to redirect them as below using .htaccess file and mod_rewrite module:
example1.com -> www.example1.com
example1.net -> www.example1.com
www.example1.net -> www.example1.com
I have achieved the first task which is this:
RewriteEngine on
RewriteCond %{HTTP_HOST} example1\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
RewriteRule ^(.*) http://www.example1\.com/$1 [L,R=301]
What about the rest?

I was able to do it myself. Don't know how well the rules are written:
RewriteEngine on
RewriteCond %{HTTP_HOST} example1\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
RewriteRule ^(.*) http://www.example1\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} example1\.net [NC]
RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
RewriteRule ^(.*) http://www.example1\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} www\.example1\.net [NC]
RewriteRule ^(.*) http://www.example1\.com/$1 [L,R=301]
Please correct or suggest a better answer

Related

Multiple domains on one host https and http

I have multiple domains on one host, my .htaccess looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]
And it works so far. Now I want only web1.com, not web2.com, to redirect to https. How do I have to change the settings? (I am also thankful for tips about how to change my "implementation" so far, I am new to .htaccess and willing to learn more about it)
Try this :
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So , your rules will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]

.htaccess rewrite for https and hidden subfolder

I need to rewrite http://www.example.com and http://example.com to
https://www.example.com.
An additional complexity is that these URLs are not pointing to the index file, but to the /folder. i.e. https://www.example.com really points to /var/www/html/folder, without showing https://www.example.com/folder in the URL bar.
I had previously managed to attain the second part with
RewriteCond %{HTTP_HOST} www.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ folder/$1 [L]
and I know that the first part can be attained with something like this:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
but somehow I just can't get both of them to work together because I'm not familiar with htaccess. Thanks in advance!
You can have a single rule to add www and http -> https redirection and then have your rewrite rule for folder forward:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!folder/)(.*)$ folder/$1 [L,NC]

Rewrite Rule in Apache/htaccess not working

I'm trying to redirect user request to my domain in .htaccess file.
My requirement is for example if user request for example.co/abc it should redirect to www.example.co/abc.
Below is my rewrite rule.
RewriteCond %{HTTP_HOST} ^example.co/Abc [NC]
RewriteRule ^(/Abc)$ https://www.example.co/$1 [L,R=301]
After adding the above two lines in my .htaccess file it is redirecting from example.co to www.example.co
I tried in different ways still not getting where i'm doing mistake.
Any help would be appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ http://www.example.co/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{www.example.co} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.co [NC]
#RewriteRule ^(/*)$ https://www.example.co/$1 [L,R=301]
RewriteRule ^/?(Abc)/?$ https://www.example.co/$1 [L,R=301]

.htaccess always redirect non-www to www except subdomains?

I searched before posting this but couldn't find a topic that fits my needs. My site is https only and I want to always redirect any request of https://domain.com to https://www.domain.com .. EXCEPT when somebody accesses https://username.domain.com then I need it redirected to https://www.domain.com/user?username=...
Right now I have it like this:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/user?username=%1 [L,QSA]
But it's not working with the non-www redirect to www. What part do I need to change and how?
I checked the other answer but it doesn't work as soon as I add the /user? rule...
You can have your rules as this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com/user?username=%1 [L,QSA,R=302]

Rewrite all subdomain URLs with .htaccess

I have a domain test.com and a subdomain sub.test.com.
Now, I need to rewrite the URL sub.test.com to test.com/?state=sub, and the URLs like sub.test.com/content/ to test.com/content/?state=sub, and sub.test.com/content/proceed/ to test.com/content/proceed/?state=sub.
The rule for the first URL worked, something like this:
RewriteCond %{HTTP_HOST} !^www\.test\.com
RewriteCond %{HTTP_HOST} !^webmail\.test\.com
RewriteCond %{HTTP_HOST} !^m\.test\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.test\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !robots.txt.*
RewriteRule ^$ http://www.test.com/?state=%1 [P]
Any idea how to do this?
This rule should work for you:
RewriteCond %{HTTP_HOST} !^(?:www|webmail|m)\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.test\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(index\.php|robots\.txt) [NC]
RewriteRule ^ http://www.test.com%{REQUEST_URI}/?state=%1 [P]