1

For example my site: www.example.com

When anyone visits my site, I want them all to redirect to www.example.com/blog

But when I open my site it should not redirect me to blog, it should work normally. So how can I achieve the above using .htaccess?

I am going to apply the following code to redirect all pages to blog:

RewriteEngine on
RewriteCond %{HTTP_HOST} old\www.example.com$
RewriteRule .* http://www.example.com/blog? [L,R=301]

But how can I specify my IP address in above so I can access all pages and no redirects.

1
  • I've just answered below, however, I've just realised that I might have misinterpreted your question!? So to confirm... are you wanting all other users (apart from yourself; your IP address) to be redirected to /blog/? The URL in the address bar should show /blog/?
    – MrWhite
    Commented May 2, 2015 at 17:54

1 Answer 1

3

To redirect everyone else, apart from your IP address (eg. 123.123.123.123), to the /blog subdirectory then you can use something like the following in .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteRule (.*) /blog/$1 [R=301,L]

If the requested URI does not start with /blog/ and the IP address is not 123.123.123.123 then redirect to include /blog/ at the start.

The $1 in the RewriteRule substitution is a backreference to the parenthesised pattern (.*) in the RewriteRule pattern (ie. the entire URL less the slash prefix - in htaccess).

Note also that I've removed the ? at the end of the RewriteRule substitution. This specifically removes any query string from the request. Unless that is required, it should be omitted.

I don't see any reason to test against %{HTTP_HOST} (as in your original example). (?)


My initial answer (below) is not what you require, but I'll leave it for completeness.

This version internally rewrites (as opposed to redirecting) all requests to the /blog subdirectory (the URL in the address bar does not change).

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]

This basically says... if the requested URI does not start with /blog/ then rewrite to include /blog/ at the start. Note the absence of the R flag on the RewriteRule directive. The R flag would result in an external redirect.

5
  • Of course this is an automatic up-vote!! Total trust in your answers. Looks dang good to me!
    – closetnoc
    Commented May 2, 2015 at 18:07
  • @closetnoc Well, I'm actually wondering whether I've misinterpreted the question and the OP wants an external redirect after all - just not when they access the site?! This would also tie in with the IP address reference.
    – MrWhite
    Commented May 2, 2015 at 18:27
  • I missed that too! Assuming one site on one IP address and no catch-all site, I would assume that entering an IP address should do what he wants with your code. As well, your /blog/$1 on your RewriteRule assumes that example.com/sugarshack/ would have the page example.com/blog/sugarshack/ which may not always be the case. Something to ask the OP along with some configuration probing. Regex in .htaccess is not always easy is it?? ;-)
    – closetnoc
    Commented May 2, 2015 at 19:33
  • @w3d Thank you for answer , Yes I want to specify my IP address so when I open site from my PC it should not redirect and for all other public it should redirect.. Commented May 3, 2015 at 4:39
  • @rahulbhatt I've updated my answer. (Sorry for the mix up!)
    – MrWhite
    Commented May 3, 2015 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.