Forums

Prevent browsing to sub domain (12 posts)

  1. Keyth
    Member
    Posted 6 months ago #

    Hello all. Ive searched everywhere for a way to do this but every time I add a block of text to the htaccess file I get a 500 error.

    I have a primary domain as example:
    mydomain.co.uk
    and I want to prevent a user browsing to 2 sub domains
    mydomain.co.uk/subdomaina
    mydomain.co.uk/subdomainb

    Heres my htaccess file:

    rewriteengine on
    
    # BEGIN WordPress
     <IfModule mod_rewrite.c>
     RewriteBase /
    
    </IfModule>
     # END WordPress
     rewriterule ^index\.php$ - [L]
     rewritecond %{REQUEST_FILENAME} !-f
     rewritecond %{REQUEST_FILENAME} !-d
     rewriterule . /index.php [L]
     rewriterule ^index\.php$ - [L]
     rewritecond %{REQUEST_FILENAME} !-f
     rewritecond %{REQUEST_FILENAME} !-d
     rewriterule . /index.php [L]

    How would I add allow/deny or rewrite conditions to the above to prevent loading the 2 sub domains?

    Thanks for any help you can offer :)

  2. esmi
    Theme Diva & Forum Moderator
    Posted 6 months ago #

    For starters, your rewrite rules are a mess. Try just using:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    as a proper foundation. Any additional rules should be added before the # BEGIN WordPress line.,

  3. Keyth
    Member
    Posted 6 months ago #

    Thanks esmi.
    I had been playing with a plugin which upset my htaccess file.

    Where would I place the following?
    RedirectMatch 404 /\\subdomaina(/|$)

  4. esmi
    Theme Diva & Forum Moderator
    Posted 6 months ago #

    Before the # BEGIN WordPress line.

  5. Keyth
    Member
    Posted 6 months ago #

    I read that instruction in your previous post but get a 500 error.
    Is there something wrong with the first line?

    RedirectMatch 404 /\\subdomaina(/|$)
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
  6. Keyth
    Member
    Posted 6 months ago #

    Can anybody please advise if there is something wrong with this line?

    RedirectMatch 404 /\\subdomaina(/|$)

    where subdomaina is a directory?

  7. esmi
    Theme Diva & Forum Moderator
    Posted 6 months ago #

    What are you trying to do with that line?

  8. Keyth
    Member
    Posted 6 months ago #

    Thanks for your reply esmi. I want to prevent access to subdomaina from mydomain

    mydomain.co.uk/subdomaina redirects to mydomain.co.uk unless loaded through a browser as www. subdomaina.co.uk

  9. esmi
    Theme Diva & Forum Moderator
    Posted 6 months ago #

    Try:

    RewriteCond %{HTTP_HOST} ^mydomain\.co\.uk/subdomaina$ [NC]
    RewriteRule ^(.*)$ http://mydomain.co.uk [R=301,L]
  10. Keyth
    Member
    Posted 6 months ago #

    Thanks very much.

    I have added that but still it doesnt work. I even tried adding a forward in the host control panel but neither work.

    Heres your version with actual domains:

    RewriteCond %{HTTP_HOST} ^kcpackaging\.co\.uk/polycover$ [NC]
    RewriteRule ^(.*)$ http://kcpackaging.co.uk [R=301,L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Heres the hosts version which is what messed everything up originally after adding re-directs through control panel:

    rewriteengine on
    rewritecond %{HTTP_HOST} ^www.kcpackaging.co.uk$ [OR]
    rewritecond %{HTTP_HOST} ^kcpackaging.co.uk$
    rewriterule ^polycover$ "http\:\/\/kcpackaging\.co\.uk\/" [R=301,L] #50ad27faa46d2
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteBase /
    </IfModule>
    # END WordPress
    rewriterule ^index\.php$ - [L]
    rewritecond %{REQUEST_FILENAME} !-f
    rewritecond %{REQUEST_FILENAME} !-d
    rewriterule . /index.php [L]
  11. esmi
    Theme Diva & Forum Moderator
    Posted 6 months ago #

  12. Keyth
    Member
    Posted 6 months ago #

    Well thanks for all your help esmi, but still not working!

    Im not sure if the directories would be classed as sub domains or rather just sub directories.

    Reading up on a post written by Jeff Starr on Perishable Press I found this article talking about RedirectMatch. It now works a treat but still puzzled as to why all the others did not work. Thanks again for your help.

Reply

You must log in to post.

About this Topic