Skip to main content
Rewrote answer after clarification from OP
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want toTo redirect everyone else, apart from your IP address internally rewrite(eg. 123.123.123.123), to the /blog but without externally redirecting.

Yousubdirectory then you can do this withuse something like the following in .htaccess:

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

This basically says... ifIf the requested URI does not start with /blog/ and the IP address is not 123.123.123.123 then rewriteredirect 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.

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.

No needI don't see any reason to test against %{HTTP_HOST} or specify IP addresses(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.

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

The $1 in the RewriteRule substitution is a backreference to the parenthesised pattern (.*) in the RewriteRule pattern.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

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.

Bit more explanation regarding $1 and backreference
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

The $1 in the RewriteRule substitution is a backreference to the parenthesised pattern (.*) in the RewriteRule pattern.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

The $1 in the RewriteRule substitution is a backreference to the parenthesised pattern (.*) in the RewriteRule pattern.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

edited body
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/
REwriteRuleRewriteRule (.*) /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.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

I want them to redirect to www.example.com/blog, but when I open my site it should not redirect me to blog

You want it to "redirect" without redirecting! What you really mean is you want to internally rewrite to /blog but without externally redirecting.

You can do this with something like the following in .htaccess:

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.

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.

No need to test against %{HTTP_HOST} or specify IP addresses.

Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90
Loading