Temporary redirect to maintenance page - .htaccess

I am trying to write a rule to redirect all URLs to a temporary page so that some site updation could be done, but it ends up in an infinite loop.
RewriteCond %{HTTP_HOST} ^(.*)mysite\.com$
RewriteCond %{REQUEST_URI} !^(.*)temp$
RewriteRule ^(.*)$ http://www.mysite.com/temp [R=307,L]
How to check if it's a temp page?

You need to write rule for all request except maintenance file.
.htaccess should be:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

What I do is to redirect all traffic to a maintenance.html page when it's not coming from my IP.
The first rewrite condition avoids an infinite loop.
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^172\.16\.254\.1$
RewriteRule $ /maintenance.html [R=302,L]

It works, I tried it. It will redirect all requests to a maintenance page.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
</IfModule>

Related

Redirecting subdomain to subdomain folder

I'm having trouble redirecting subdomain.domain.com to subdomain.domain.com/folder.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^(.*)$ /folder/$1 [R=301]
This just keeps redirecting. What am I doing wrong?
You created an endless rewrite loop, since ^(.*)$ also matches /folder/....
You need to add a condition to prevent the endless rewriting:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^/?(.*)$ /folder/$1 [R=301]

Redirecting visitors to an Under Construction page for few urls of the website

I need to redirect visitors(few ips can still have access which belongs to us) on few specific urls of my web site to Under Construction page.
I have used following code to do the complete site redirect to Under Construction page, but this time I need to redirect only a few urls.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^50\.40\.30\.20
RewriteCond %{REMOTE_ADDR} !^100\.90\.80\.70
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
</IfModule>
let say I need to redirect /example1/ & /example2/ pages to Under Construction page. How should I do that with .htaccess
You can use:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^50\.40\.30\.20
RewriteCond %{REMOTE_ADDR} !^100\.90\.80\.70
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule ^(example1|example2) /maintenance.html [R=302,NC,L]
Try :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^50\.40\.30\.20
RewriteCond %{REMOTE_ADDR} !^100\.90\.80\.70
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule (example1|example2) /maintenance.html [R=302,L]
You can exclude multiple pages by using the pipe char | .

301 redirect not working in .htaccess

I have two domains, autodromodifranciacorta.it and franciacortacircuit.com both pointing to the same website hosted on this IP address: 94.23.64.40.
Now i want all the content to be under one single domain, so i decided to 301 redirect all the traffic from franciacortacircuit.com to autodromodifranciacorta.it
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect is not working and i have no clue why, because the syntax looks correct to me.
The .htaccess file is processed, because if i put a typo in it, i get server error.
Wha'ts wrong with it?
Your second http_host condition is wrong and it never matches if the current host is franciacortacircuit.com, You need to use an OR condition to match against 2 diffrent hosts.
#Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]

How to redirect from non-www to www with correct page?

I am trying to 301-redirect example.com to www.example.com by .htaccess:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Problem: my-site.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
How can I accomplish that?
UPDATE: this is the complete .htaccess for my Joomla-site (comments stripped):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
###### this is the code in question...
RewriteCond %{HTTP_HOST} ^example$ [NC]
RewriteRule ^(.*)$ http://www.example/$1 [R=301,L]
# RewriteBase /
## Begin - Joomla! core SEF Section.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
# take care of sitemap.xml
RewriteCond %{REQUEST_URI} ^/sitemap.xml
RewriteRule .* /index.php?option=com_xmap&view=xml&tmpl=component&id=1&format=html [L]
I moved my two lines up just below RewriteEnigine On - but had the same problem again. Clueless.
Problem: example.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
I'm going to guess that you have other rules in your htaccess file, one that routes everything to index.php. You need to make sure that your redirect rule is before any other rule that you have.
After checking your htaccess file, you need to add an L flag at the end of your first rule:
RewriteRule .* index.php [F,L]
Because without it, the redirect gets applied even if the first rule gets applied (e.g. it gets rewritten to index.php with a 403 response waiting in the queue).
Firstly uncomment line
# RewriteBase / //Remove #
Below the above line write this code
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I guess I'm already in the wrong for asking for clarification but you can just delete this.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
I see you added an exclamation point and I've seen this redirect elsewhere without it. What does the exclamation point do in this case or is that incorrect?
Have a great day.

A simple mod re-write rule doesnt work with htaccess

I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]
You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]

Resources