Rewrite all subdomain URLs with .htaccess - apache

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]

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]

Wildcard 301 redirect using .htaccess

I moved a website from http://lifeworkslearningcenter.com to http://lifeworks.life. I set up seven 301 redirects for individual URLs that are working fine, as well as a simple 301 to redirect the base URL to the new base URL.
Now I need to set up a wildcard for the remaining URLs, typos, etc. What is happening right now is this: any URL not specifically covered in the 7 redirects will redirect to the new base URL, then add the previously entered trailing URL, and results in a 404. Like this:
http://lifeworkslearningcenter.com/incorrect-url
https://www.lifeworks.life/incorrect-url
Here is my .htaccess code, including three previous attempts at wildcard redirect statements:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^about-lifeworks$ "https\:\/\/www\.lifeworks\.life\/team\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^services$ "https\:\/\/www\.lifeworks\.life\/services\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^methodology$ "https\:\/\/www\.lifeworks\.life\/approach\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^sign-up$ "https\:\/\/www\.lifeworks\.life\/enroll\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^rates-and-policies$ "https\:\/\/www\.lifeworks\.life\/policies\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^contact-us$ "https\:\/\/www\.lifeworks\.life\/contact\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^blog$ "https\:\/\/www\.lifeworks\.life\/blog\.html" [R=301,L]
Redirect 301 / http://lifeworks.life/
# FAILED ATTEMPTS AT WILDCARD REDIRECT
#RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
#RewriteRule ^$ "https\:\/\/www\.lifeworks\.life\/index\.html" [R=301,L]
#RedirectMatch 301 /(.*) /$1
#RedirectMatch ^/(.*)$ http://lifeworks.life/$1
Your wildcard redirect should look like this and placed at the bottom of your other rules.
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]
If you need it to redirect based on file not found (404) you can have these rules. And place these at the bottom of your rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]

htaccess Redirect everything except subdomain and folder

I am trying to redirect my whole domain www.example.de to www.domain.de/artikel.html except for 3 subdomains (and all whats in there) and 2 folders (and everything whats in there):
RewriteCond %{REQUEST_URI} !^/folder1/subfolder2/?$ [NC]
RewriteCond %{REQUEST_URI} !^/folder2/?$ [NC]
RewriteCond %{HTTP_HOST} !^static1\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static2\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static3\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.de [NC]
RewriteRule ^(.*)$ http://www.domain.de/artikel.html [R=301,L]
But it just redirects everything with www.example.de to www.domain.de/artikel.html but does not redirect anything for http://example.de.
Anyone a hint or solution for me??
Thanks!
the problem is with this condition which causes redirections to occur only for subdomains:
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.de [NC]
replace it with :
RewriteCond %{HTTP_HOST} (\.|^)example\.de [NC]
or you can completely remove it if your htaccess is attached to only one site (example.de)
your htaccess would be:
RewriteCond %{REQUEST_URI} !^/folder1/subfolder2/?.*$ [NC]
RewriteCond %{REQUEST_URI} !^/folder2/?.*$ [NC]
RewriteCond %{HTTP_HOST} !^static1\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static2\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static3\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} (\.|^)example\.de [NC]
RewriteRule .* http://www.domain.de/artikel.html [R=301,L]

Canonical 301 Redirect for 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

Redirect from few url's (both www and non-www) to original one

How to shorten this expression? i want instead of using 2 lines just check whatever domain is something different from mysite.com e.g. mysite.org or www.mysite.org with or without www and redirect.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
Also is it faster to use [OR] or do like this
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
P.S. why this works as case insensetive even when i dont use [NC]?
You may want to check Apache documentation on canonical hostnames, your rules can be expressed as a negation
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
And for the rules matching without [NC] , it probably depends on what the browser sends to the server.