How do I remove a subfolder from my URLs?

Viewed 28

There are a handful of questions about this but none of the answers seem to work for me.

How do I edit my .htacess file to go from this: https://www.website.com/removethis/keepthis.html

To this: https://www.website.com/keepthis

I have successfully removed the .html portion with the following lines:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

It's removing the subfolder that doesn't seem to work. I've tried to make sense of the available documentation but I'm pretty new to this. If it matters my website is hosted on Namecheap (cPanel). Any help would be much appreciated!

1 Answers

Try something like the following in the root .htaccess file. Note that you must already be linking to the canonical URLs in your HTML source (see below*1) - it sounds as if you might not be?

Options -MultiViews
DirectoryIndex index.html

RewriteEngine On

# Redirect "/removethis/index.html" to "/"
# - Only direct requests should be redirected
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^removethis/(.*/)?index\.html$ /$1 [R=302,L]

# Redirect "/removethis/keepthis.html" to "/keepthis"
# - Only direct requests should be redirected
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^removethis/(.*/)?([^/.]+)\.html$ /$1 [R=302,L]

# Rewrite "/" to "/removethis/"
# Or "/foo/bar/baz/" to "/removethis/foo/bar/baz/"
# - Only if the target directory exists
RewriteRule %{DOCUMENT_ROOT}/removethis/$1 -d
RewriteRule ^(|[^.]+/)$ removethis/$1 [L]

# Rewrite "/keepthis" to "/removethis/keepthis.html"
# - Only if the target file exists
RewriteRule %{DOCUMENT_ROOT}/removethis/$1.html -f
RewriteRule ^([^.]+)$ removethis/$1.html [L]

This handles the .html file extension and the hidden subdirectory (ie. /removethis) in a single directive.

The redirects to remove the subdirectory (/removethis) and .html file extension must only be processed on direct requests and not rewritten requests by the later rewrites (otherwise you'll get a redirect loop). The check against the REDIRECT_STATUS environment variable ensures this.

The URL-path can consist of multiple path segments. eg. /foo/bar/baz is internally rewritten to /removethis/foo/bar/baz.html. And externally redirected in the other direction. Special treatment is given to index.html (the DirectoryIndex document).

*1 You need to make sure you are already linking to the canonical URLs in your HTML source. The redirects here are only to preserve SEO if you are changing an existing URL structure.

For example, you should already be linking to canonical URLs of the form:

  • / - document root (homepage)
  • /keepthis - Files do not end in a trailing slash
  • /path/to/file - Files do not end in a trailing slash
  • /path/to/dir/ - Directories end in a trailing slash

And not:

  • /removethis/index.html - document root (homepage)
  • /removethis/keepthis.html
  • /removethis/path/to/file.html
  • /removethis/path/to/dir/index.html

The redirects should ultimately be 301 (permanent), but only change this after you have confirmed it works as intended.

Related