Reputation: 1720
Is it possible to 'remove' a folder from the URL so if somebody types http://www.example.com/dummy/public/index
into their browser address bar, I can strip the 'public' folder, so the URL reads http://www.example.com/dummy/index
I essentially want to hide the 'public' folder so it never appears in the URL.
I have this htaccess at the root of my site which is www.example.com/dummy/
RewriteEngine on
RewriteRule ^(.*) public/$1 [L]
This is the htaccess inside my public folder which is located at www.example.com/dummy/public/
Options -MultiViews
# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On
# Prevent people from looking directly into folders
Options -Indexes
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I tried changing the last RewriteRule to RewriteRule ^(.+)public/(.*)$ index.php?url=$1$2 [QSA,L]
but when I type www.example.com/dummy/public/index
the page loads but the browser address bar still shows 'public' in the path.
Is it possible to do what I'm attempting?
I've seen a few SO answers that claim to accomplish this such as .htaccess: remove public from URL and URL-rewriting with index in a "public" folder, but none of them work for me.
Upvotes: 2
Views: 3204
Reputation: 784978
If you want to remove public/
from your URLs then rule should be placed inside /public/.htaccess
since all the requests with URI starting with /dummy/public/
will be managed by rules inside /public/.htaccess
.
Change your /public/.htaccess
with this:
# Prevent people from looking directly into folders
Options -Indexes -MultiViews
# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On
# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
RewriteRule ^ /%1%2? [R=301,L,NE]
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Upvotes: 3
Reputation: 4907
Fear not! For the answer is yes. Use the following rule in your .htaccess
to remove the /public/
folder from your URLs:
RewriteEngine On
RewriteRule ^(.*)/public /$1 [R=301,L]
This will leave you with the desired URL of http://www.example.com/dummy/index
and it achieves this using a 301
permanent redirection. For testing purposes I suggest you change this to 302
as this will make it temporary, once happy, change it back.
Make sure you clear your cache before testing this.
Upvotes: 0