We have add-on domains to the same directory of our website, 1 of the domain is for us to access and test the website. I want to activate Maintenance mode excluding our test domain so we can access the site and do our tests.
%{HTTP_HOST} is to test the domain name. If the HTTP_HOST is not the test.com domain, and the REQUEST_URI is not the maintenance.php (to prevent a redirect loop) redirect the traffic to the maintenance page.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.test\.com [NC]
RewriteCond %{REQUEST_URI} !^/maintenance.php$
RewriteRule ^/?(.*) /maintenance.php?$1 [R=301]
Related
I need redirect only domain, eg. www.example.com, but any subdomain cannot be redirected. Also, if I have files on www.example.com/folder/file.pdf, then those files should be available under the main domain. How to do it with a 301 redirect?
For eg.
www.example.com => redirect
www.sub.example.com => not redirected
www.example.com/folder/file.pdf => not redirected
You can do this using mod_rewrite at the top of your .htaccess file. To specifcally match the domain name, you need a condition that checks against the Host HTTP request header.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^path/to/source\.html$ https://example.com/path/to/target.html [R,L]
If you are redirecting to the same domain, then you don't strictly need to specify a scheme+hostname in the substitution - but without knowing your server config, it is more reliable to do so.
To redirect all URL-paths to a different domain, but with the same URL-path, except the specific file you mentioned:
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteCond %{REQUEST_URI} !^/folder/file\.pdf$
RewriteRule (.*) https://example.net/$1 [R,L]
UPDATE:
The point is I want redirect only main domain. Nothing else - no subdomains or paths under main domain. Main domain will be redirected to Facebook page.
If you simply want to redirect the document root of the main domain, eg. example.com/ then you don't need to make any exceptions for folders/files you don't want to redirect, as you are only redirecting a single URL.
For example:
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^$ https://www.facebook.com/page [R,L]
Is it possible to make a conditional redirect using .htaccess in a way that if somebody access my domain directly (example.com) he will be redirected to a subdomain (subdomain.example.com) or may be to another domain, but if someone accesses it through a particular URL (example.com/magic) he get's redirected to example.com i.e. main domain.
In other words, I wanted my users to access the main domain (example.com) only if they are using example.com/magic.
My htaccess is presently redirecting all users of main domain to a subdomain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/sub\.example\.com" [R=301, L]
I couldn't find though, how to put the condition in my htaccess.
You may use this rule to redirect every URI that is not /magic or /magic/ to a sub-domain:
RewriteEngine On
# redirect /magic
RewriteRule ^magic/?$ / [L,NC,R=301]
# redirect anything except /magic
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^(magic/?)?$ https://sub.example.com [R=301,L,NE]
We host a number of websites on our server. Our main site, let's call www.domain.com.
Anyway, we noticed that the other sites we host, etc, all point back to our main site (domain.com) when https:// is put in front of their domain. Part of the problem is that these are indexing in Google as well.
I've wondering how I can redirect all these in the htaccess - please help!
Add these rules to the htaccess file in your www.domain.com document root, preferably above any other routing rules that may be there:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
I have a subdomain for our old store of store.mydomain.com . We just launched a new store at the root domain www.mydomain.com
How can we redirect all traffic from store.mydomain.com to www.mydomain.com for all traffic EXCEPT for those from a specific IP?
In other words we are trying to maintain internal access to store.mydomain.com because we have some things we still need to do with the old store (customer records, orders, etc...) but we don't the general public to be able to access any URI on the old store.mydomain.com subdomain. We want all of those people to be redirected to www.mydomain.com
The following works to redirect ALL traffic, but unsure of how to do the IP condition check.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^store.example.com$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
Try:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78%
# etc...
RewriteCond %{HTTP_HOST} ^store.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
So this will redirect for everyone except when the request comes from the IP:
1.2.3.4
12.34.56.78
I have a multi-subdomain multi-lingual website setup and I need to redirect a language subfolder of a certain domain. eg. au.domain.com/us/request_uri to au.domain.com/en/request_uri. This rule needs to be ignored on other domains. All domains are run off the same codebase and all use the one htaccess file (drupal install with domain access module). This shouldn't affect how the htaccess rule is set though.
Here is one way to do it:
RewriteEngine On
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Redirects:
http://au.domain.com/us/anything to
http://au.domain.com/en/anything/
UPDATED
RewriteEngine On
RewriteRule http://au.domain.com/us/^(.*)$ http://au.domain.com/en/$1 [L,R=301]
Only http://au.domain.com/us/anything will be redirected to
http://au.domain.com/en/anything/
OR
RewriteEngine On
RewriteCond %{HTTP_HOST} ^au.domain\.com$ [NC]
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Hope this helps.