.htaccess - Redirect for all users, except me - apache

Please help me with the htaccess. I tried to create a rule whereby a user visiting a particular web page is automatically redirected to another page on the site, but if I visit it page, this rule does not work for me (for a specific IP address).
LiteSpeed ​​installed on the server, and I think fits the rule for Apache (correct if I'm wrong, I just do not understand the server software).
Sorry for bad english.

This is a 302-Moved Temporarily redirection.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^xxx\.xxx\.xxx\.xxx
RewriteRule \.html$ /anotherPage.html [R=302,L]
where xxx.xxx.xxx.xxx is your ip and anotherPage.html is the redirection page.

Related

Apache2 301 Redirect Not Working

I have attempted to read various documentation, as well as other answers online, and I cannot find a solution to my problem.
Here is my issue:
I have a permanent www to non-www redirect setup to point traffic from www.domain.com to domain.com
This works, but I am having issues when trying to visit specific urls. For example:
We have three PDFS that we would like people to be able to download via:
domain.com/paper/somepdfname.pdf
When i visit the url like this it works, but if I try to visit something like this:
www.domain.com/paper/somepdfname.pdf
The browser gets redirected, and strips one of the slashes out, resulting in "the site cannot be reached error". This is the url I end up with in the browser:
domain.compaper/somepdfname.pdf
I think its pretty clear that I need to somehow make sure there is a forward slash put before the paper, but I do not know how to do this. Below you will find my .htaccess directive for handling the redirect:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]

How to 301 redirect when a user's IP address is not authorized?

I'm setting up a development server on one of my subdomains. I'm controlling access to the site using the visitor's IP address. When I see a visitor that is not authorized, I want to 301 redirect them to the main website. I've got a temporary solution using the following .htaccess directive:
ErrorDocument 403 http://www.mymainsite.com/
But my understanding is that's a 302 redirect. I want to do a 301.
In looking for a solution I came across the following:
RewriteCond %{HTTP:Authorization} !=""
RewriteRule ^$ http://www.mymainsite.com [R=301,L]
It does not appear to be working, however. Just for reference, I'm blocking on IP, not a login, so this are my settings:
Order deny,allow
Deny from all
Allow from 12.2.120.233
Anyone know how to get this working as a 301?

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]

htaccess RewriteCond help for new development site

I have having a real problem here and I am hoping someone can help me out - let me explain:
I have a site - domain.co.uk - for the past 6 months this site has been sat on holding pages so google gets to know the domain.
I also have domain.com and domain.org - these were also set up at the same time.
We have a new website under development using a Drupal multisite install where all of the multisites are sub-domains.
Here is my problem!! The new website is now under development and I need to add various rules to the htaccess to redirect back to the holding pages but these rules are new to me and I dont really have a clue what I am doing!
This is what I need to do:
The primary domain is domain.co.uk.
I need to redirect all users requesting domain.co.uk to domain.co.uk/holding/index.php,
but I need to put an IP address exclusion in for us and the developers for testing the new site so we can see what is going on with the new website.
The website also has .com alias and this needs to go to domain.co.uk for all requests.
So here is what I have so far and it does not work as it should:
// I set this to re-write anything except the subdomains I want to see
RewriteCond %{HTTP_HOST} !^(www.domain\.co\.uk)|(subdomain1.domain\.co\.uk)|(subdomain2.domain\.co\.uk)|(subdomain3.domain\.co\.uk)$ [NC]
RewriteRule (.*)$ http://www\.domain\.co\.uk%{REQUEST_URI} [R=302,L]
// then I have put in some rules for checking the ip's and producing a redirect that does not match earlier requests.
RewriteCond %{REMOTE_HOST} !^(xx\.xxx\.xxx\.xx)|(xx\.xx\.xx\.xx)$
RewriteCond %{REQUEST_URI} !/holding/index\.php$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css) [NC]
RewriteRule ^(.*)$ /holding/index\.php [R=302,L]
The result so far is:
if a general user goes to the domain.co.uk it redirects to the holding page - GOOD
if a user from the 'excluded ip's address list goes to domain.co.uk they also get redirected - BAD - but if they go to .domain.co.uk/index.php the Drupal site loads with the new design.
If a user from the excluded ip's list goes to subdomain1.domain.co.uk they get redirected to the holding pages - BAD- this, I think, should not happen due to the 1st rule but I am totally lost.
Anyone got any ideas how to get this working so that a user with an allowed ip can access the new website and all it's subdomains but any general user is directed to the holding pages.
Many thanks - my head is about to explode!!
${REMOTE_ADDR} is the IP Address, ${REMOTE_HOST} is the hostname if available.
You could also temporarily add ${REMOTE_ADDR} into the redirect to the holding page to check that you are putting the right addresses in, e.g.:
RewriteRule ^(.*)$ /holding/index\.php?ip=${REMOTE_ADDR} [R=302,L]

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.