Mobile app version of vmapp.org
Login or Join
Welton855

: Want to redirect my site all pages to /blog 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

@Welton855

Posted in: #Htaccess #Redirects

For example my site: example.com
When anyone visits my site, I want them all to redirect to 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} oldwww.example.com$
RewriteRule .* 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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Welton855

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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/ [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 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/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme