Redirect in certain hours .htaccess issue - .htaccess

I would like the traffic on site1.com to be redirected to site2.com only from 1pm to 2pm and from 11pm to 5am.
This redirect only if users are from Italy.
Is it possible?
I've tried something like this... but it does not work properly.
Order deny,allow
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0600
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1800
RewriteRule ^page\.html$ page.day.html
RewriteRule ^page\.html$ page.night.html

Couple of things to consider here:
Your rewrite conditions only apply to one rewrite rule
you could tie your redirects to certain IP ranges that are known to come from Italy but this isn't very reliable. Consider a code level solution for the location aspect instead. But know that it will never be perfect.
There is no need to use %{TIME_MIN} if you're only interested in hours
All that taken into consideration, I would try something like this:
Order deny,allow
RewriteEngine on
RewriteCond %{TIME_HOUR} >13
RewriteCond %{TIME_HOUR} <14
RewriteRule (.*) http://site2.com/$1 [QSA,L,R=302]
RewriteCond %{TIME_HOUR} >23
RewriteCond %{TIME_HOUR} <5
RewriteRule ^(.*)$ http://site2.com/$1 [QSA,L,R=302]
Note that I am forcing a 302 redirect (moved temporarily) to indicate that the redirect is not permanent.

Related

Using two RewriteRule in .htaccess

I have two RewriteRule in my .htaccess file which is hosted at godaddy.First one is for redirecting http to https and works fine.Second one is for using slash instead of question marks in URL but not working.Where am I wrong?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
Edit:To be more clear,my URL now looks like :
sosyosapien.com/index.php?postt=23&konu=what_is_sosyosapien&kategori=science
But I want it to look like
sosyosapien.com/index.php/23/what_is_sosyosapien/science
But my code only does http to https redirect and cant change the URL in the way I want.Sorry I dont have enough knowledge of .htaccess' working principle.Can you please give me a code to do what I need?
You most likely do not want to add this RewriteRule to your .htaccess. It is incorrect and it is missing capturing groups that it has been referred to:
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
If you really have to keep this, you might find out what $1, $2 and $3 are that you might want to redirect. Then, you could change this ^low/$ based on that.
Maybe, just add this, and it might work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sosyosapien\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteCond %{QUERY_STRING} postt=(.*)
RewriteRule ^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$ ^$1\/$2\/$3\/$4$ [R,L,302]
</IfModule>
This RegEx might help you to simplify your expression, as you wish, since I'm not sure what types of conditions you might have:
^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$
Graph
This graph shows how the RegEx works:
You might need to restart apache:
sudo apachectl restart
You also want to clear your browser cache, every time that you would make a change in .htaccess.

Language Rewrite .htaccess

Sigh...
I tried constructing some rewrite rules based on the following Stack Overflow questions:
Question 1
Question 2
My .htaccess currently looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
RewriteRule ^(en|fr|es|de)/(.*)$ $2?locale=$1 [L]
and I've tried all sorts of variations (including the exact answers given in the questions).
As it stands my URL is redirecting to:
/en/?locale=en
When I look at the root with what looks like an infinite redirect loop in Firefox ('The page isn't redirecting properly').
As you've probably guessed I want it to rewrite to /en/ on the root and for it to look like that in the browser. But I want the real url to be /?locale=en. There is an index.php there on the root. Maybe I need another rule to take that into account?
I don't know, I'm really tired and exasperated and .htaccess has always been my downfall. Any insight appreciated.
You need to make sure there's no locale query string in your first rule:
RewriteCond %{QUERY_STRING} !locale=
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
Or you can try:
RewriteCond %{THE_REQUEST} \ /+(?!(en|fr|es|de)/).*
RewriteRule ^(.*)$ /en/$1 [R,L]

URL redirect for certain posts to subdomain with htacess

I'm moving a blog to a subdomain for a fresh start, so I need the links to old posts to redirect from name.com to v1.name.com. But, there will be new posts at name.com that need to be ignored. I've changed the URL structure on the new blog installation so I can say only URLs with a month and year need redirected, but I haven't been able to get that working, and all the examples I've found are looking at changing the end of the URL, not the middle. What am I doing wrong with what's cobbled together below?
(And is there any way to achieve this without changing the URL structure on the new blog? I'd want all posts before /2013/12/ redirected, but I'm not aware of any way to specify that.)
RewriteEngine on
RewriteRule ^name.com/([0-9]{4}/[0-9]{2})/(.*)$ v1.name.com/$1/$2 [NC, R=301]
OK, you needed to move the hostname match into a separate condition, and add the http:// bit in front of your substitution:
RewriteEngine on
RewriteCond %{HTTP_HOST} name.com
RewriteRule ^([0-9]{4}/[0-9]{2})/(.*)$ http://v1.name.com/$1/$2 [NC, R=301]
For bonus points, you said you wanted to match anything before 2013/12 - if I interpret that correctly to mean you want all posts from 2013 including December, you can do this:
RewriteEngine on
RewriteCond %{HTTP_HOST} name.com
RewriteCond %{REQUEST_URI} ^/(1[0-9]{3}|200[0-9]|201[0-3])
RewriteRule ^([0-9]{4}/[0-9]{2})/(.*)$ http://v1.name.com/$1/$2 [NC, R=301]
And if you want to exclude December for 2013:
RewriteEngine on
RewriteCond %{HTTP_HOST} name.com
RewriteCond %{REQUEST_URI} ^/(1[0-9]{3}|200[0-9]|201[0-3])
RewriteCond %{REQUEST_URI} !/2013/12
RewriteRule ^([0-9]{4}/[0-9]{2})/(.*)$ http://v1.name.com/$1/$2 [NC, R=301]

.htaccess: Rewrite to subdirectory with a twist

I have domain-a.com and domain-b.com. The host runs a multi-site Contao installation with two sites to which both domains are assigned respectively. Both sites are supposed to have a Wordpress blog in a /blog subfolder. Of course they realistically can not, so the first one is domain-a.com/blog and the second is domain-b.com/blog-b.
Wrapping my head around .htaccess has proven to be really difficult for me and I just can't figure out how to get this logic to work:
if
domain is domain-b
and
request_uri starts with /blog
rewrite to domain-b/blog-b/$1
I tried like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.comt [NC]
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^/(.*) /blog-b/$1
Does not work. How is it done?
Ah, I think the $1 is capturing the /blog/ in the incoming url, so it's doing /blog-b/blog/...
Try this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.com [NC]
RewriteRule ^/blog/(.*)$ /blog-b/$1 [NC,L]
Also, depending if you have a RewriteBase, the leading slash in the RewriteRule may need to be removed.

.htaccess add trailing slash after domain

How can I re-inforce the forward slash after domain - situation like this:
http://www.domain.com
to become
http://www.domain.com/
At the moment I have something like this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] [AND]
RewriteCond %{HTTP_HOST} !^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
</IfModule>
I've tried different things, but cannot get it to work.
http://example.com and http://example.com/ are the same URL. You don't need to do anything at all. See RFC 3986 for details.
Edit: Because this is inexplicably being voted down despite being completely correct, please see section 6.2.3 in particular:
the following four URIs are equivalent:
http://example.com
http://example.com/
http://example.com:/
http://example.com:80/
Most if not all browsers add the / by default. However browsers recently decided to hide the http:// and the / after from the user (it however still sends them). The other thing is that mod_dir which is installed in pretty much all apache installations already does a 301 redirect if the / is not present. So I thing you want to solve something that isn't a problem to begin with.

Resources