• 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 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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.,

    Thread Starter Keyth

    (@keyth)

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

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

    Before the # BEGIN WordPress line.

    Thread Starter Keyth

    (@keyth)

    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

    Thread Starter Keyth

    (@keyth)

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

    RedirectMatch 404 /\\subdomaina(/|$)

    where subdomaina is a directory?

    What are you trying to do with that line?

    Thread Starter Keyth

    (@keyth)

    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

    Try:

    RewriteCond %{HTTP_HOST} ^mydomain\.co\.uk/subdomaina$ [NC]
    RewriteRule ^(.*)$ http://mydomain.co.uk [R=301,L]
    Thread Starter Keyth

    (@keyth)

    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]
    Thread Starter Keyth

    (@keyth)

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Prevent browsing to sub domain’ is closed to new replies.