Search code examples
phphtml.htaccesshttp-redirect

htaccess redirect subdomain wrong url


I have a website for which I redirect to the public directory as I am using lavarel. However, today I want to create a different website on a subdomain I created "sub.domain.com" but when I visit the url "sub.domain.com" it redirects to "sub.domain.com/public" I dont want this new website on my subdomain to use the htaccess redirect for public that uses my main domain website. I want my sub domain to have its own website and not use the redirect of public.

here is what i have on my htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /public/ [B,L]
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php81†package as the default “PHP†programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php81 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

I figured that if I just created a director called "events" it would use the htaccess on root and redirect to public which is the problem I am having even when I didnt create a directory, I created a sub domain which has a directory named "events.domain.com" inside root.


Solution

  • Create another .htaccess in the subdomains document root (ie. the subdirectory inside the main domains document root) and disable (or enable) the rewrite engine:

    RewriteEngine Off
    

    This prevents the mod_rewrite directives in the parent .htaccess file (that are processed along the directory path) from being processed.

    You can also enable the rewrite engine (if you need to do some rewriting/redirecting for the subdomain), since mod_rewrite is not inherited by default.

    Although, if the subdomain does not have anything to do with the main domain then it should not be pointing to a subdirectory off the main domain’s document root in the first place. You then wouldn’t have these issues.