htaccess question

lydia09

New Member
Messages
64
Reaction score
0
Points
0
Hi everybody,
I've created a subdomain on my web site, which I plan to put pages in, but I don't want to make that subdomain public for a while, so for the moment, I'd like to block access to the subdomain from all IP addresses except mine with a temporary redirect to a placeholder file in the document root of the subdomain.

I thought .htaccess was the way to do it, and I made up this file in the document root of my subdomain:

Options -Indexes
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^000\.000\.000\.000
RewriteRule http://subdomain.maindomain.x10hosting.com/* http://subdomain.maindomain.x10hosting.com/index.html [R=302,L]

Unfortunately, the redirect doesn't seem to be happening; the files seem to be accessed as normal. Is .htaccess the right way to do this, and if so, what am I doing wrong? I've tried Googling, and this seems right, but clearly it isn't. Can anyone give me a hand, please?

Thanks,
Lydia
 

Awesomexr

New Member
Messages
118
Reaction score
4
Points
0
You can do this in cPanel, go to "Subdomains" and then click "Manage redirection" next to the subdomain you wish to redirect.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
If the subdomain maps to a subdirectory of public_html, the simplest way is to use:
Code:
Order Deny, Allow
Deny from all
Allow from XXX.XXX.XXX.XXX
though that won't give you a redirect.

As for RewriteRule, you can't match the URL scheme, host or query string in the pattern; you can only match against the path portion of the URL. Instead, you'll need to check the server name in another RewriteCond. Also, %{REMOTE_HOST} is the remote hostname, not IP. You'll most likely want to use REMOTE_ADDR.

Code:
RewriteCond %{REMOTE_ADDR} !=XXX.XXX.XXX
RewriteCond %{SERVER_NAME} ^subdomain\.
RewriteRule . /index.html [R=302,L]
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
RewriteCond %{REMOTE_ADDR} !=XXX.XXX.XXX
RewriteCond %{SERVER_NAME} ^subdomain\.
RewriteRule . /index.html [[B]R=302[/B],L]

Doesn't the Redirect directive give you a loop?
 

lydia09

New Member
Messages
64
Reaction score
0
Points
0
Thanks misson,
That works well, I appreciate the help. Now, this seems right too, but I'd just like to double-check so I know I understand-- if I was just doing a temporary redirect for a folder and its contents (if the page I wanted to redirect to was in that folder), without worrying about a subdomain, it would be:

Code:
RewriteCond %{REMOTE_ADDR} !=XXX.XXX.XXX
RewriteRule . index.html [R=302,L]

right?

Thanks,
Lydia

PS. Awesomexr, thanks for the advice, but that isn't quite the redirect type I want, I don't want to redirect everything, just everything except my IP.
PPS. descalzo, I haven't run into a loop, where have you run into one, just as an FYI?
Sorry I forgot to acknowledge you both in my original post! :)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Doesn't the Redirect directive give you a loop?
Yes it will.

Lydia, you'll need another rewrite condition, something like:
Code:
RewriteCond %{REQUEST_URI} !^/index\.html$
...

I say "something like" because the exact pattern you use depends on the top-level URL path and how you want to handle URLs for pages in sub-folders.

if I was just doing a temporary redirect for a folder and its contents (if the page I wanted to redirect to was in that folder), without worrying about a subdomain, [...]
Basically correct; if you don't care about the server name used in the request, remove that test. You'll also need to adjust the patterns as the request URL will vary, depending on the server name used (e.g. subdomain.maindomain.x10hosting.com/foo/bar vs. maindomain.x10hosting.com/subdomain/foo/bar).
 
Top