Owen Kavanagh Owen Kavanagh - 1 month ago 13
Apache Configuration Question

.htaccess: How to remove subfolder's subfolder in URL?

I'm trying to redirect this URL

www.domain.com/~username
to
www.domain.com/~username/public
but remove the
/public
from the URL.

This is my
.htaccess
file

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /~username/
RewriteRule !^public/ public%{REQUEST_URI} [L]

RewriteCond %{THE_REQUEST} ^GET\ /public/
RewriteRule ^public/(.*) /$1 [L,R=301]

</IfModule>


But I'm getting this error:

Not Found

The requested URL /~username/public/~username/ was not found on this server.


Any help appreciated!

Answer

You can try this code:

RewriteEngine on
RewriteBase /~username/

RewriteCond %{THE_REQUEST} ^GET\ /public/ [NC]
RewriteRule ^public/(.*)$ $1 [L,R=301,NE]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Comments