Search code examples
.htaccesshttp-redirecturl-rewritingrootsubdirectory

htaccess rewrite subdirectory as root and also redirect if the subdirectory is accessed directly


I have URLs like this

https://example.com/public
https://example.com/public/faq.php

I want to rewrite it like this

https://example.com
https://example.com/faq.php

Rule I am using is this

RewriteEngine On
Options +FollowSymLinks
RewriteRule ^$ public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]

This rule works very well but it doesn't redirect if someone access "https://example.com/public/" or "https://example.com/public/faq.php"


Solution

  • Solution

    I have found a workaround for this. I have added below code to redirect if the public directory will be accessed directly.

    RewriteCond %{THE_REQUEST} /public [NC]
    RewriteRule .* / [L,R]