Wildcard 301 redirect using .htaccess - apache

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]

Related

force all user to non-www and https

I want to redirect all my visitors to https://example.com
I have found two codes to implement this, Please tell me which one should I use?
First one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Second One:
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

Using htaccess to redirect all pages but one to another domain

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Htaccess: rewrite all requests to https and domain to subfolder without listing subfolder

I want to achieve the following below.
Rewrite (www.example.com, example.com) to (https://example.com/folder) without showing /folder in the url i.e (https://example.com)
Also want other requests to (www.example.com/other-folders/..., example.com/other-folders/...) to rewrite to https i.e (https://example.com/other-folders/...)
At the moment, I have this below:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*)$ /folder/$1
www.example.com - Works perfectly
example.com - Works but returns this error "incorrect access detected, this server may be access only through https://example.com/folder... then redirects but shows /folder
www.example.com/other-folders and example.com/other-folders - Doesn't work
Try :
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule (.*)$ /folder/$1

.htaccess rules causing internal server error yet they look right

My server's. htaccess rules look like this:
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]
But for some reason none it causes a server internal error, I'm not sure if they are all 100% correct either. Like the last rule, should that too use ^(.*)$ at the start and $1 at the end
Same for the flags, not too sure if they are all correct.
Basically I am trying to make:
testing.examplewebsite.co.uk and www.testing.examplewebsite.co.uk go to https://testing.examplewebsite.com
test.examplewebsite.co.uk and www.test.examplewebsite.co.uk go to https://test.examplewebsite.com
Some other subdomains like above
examplewebsite.co.uk and www.examplewebsite.co.uk go to https://examplewebsite.com
Make all used domains, for example stackoverflow.examplewebsite.co.uk and www.stackoverflow.examplewebsite.co.uk both be sent to https://examplewebsite.com
You have a typo in all of your conditions. the 2nd conditions in your lines is missing a . before uk. ^testing.examplewebsite.co.uk$ [NC]
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]

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.