Remove /web part from a Composer based Drupal site

By default when you create your Drupal project using composer it will create project under /web directory.

When you’re using shared hosting for your Drupal website which points your domain to /public_html folder and you’re not allow to change that pointing directory

Now whenever your targeted audience will have to visit your website they have to open it like this and which is not a good user experience to add /web at the end of your url to view Home page of your website.

So, How can we serve our website from /public_html/web folder but no need to append /web in URL by your targeted audience?

1. Open your settings.php file and add the following code:

if ( isset($GLOBALS[‘request’]) && ‘/web/index.php’ === $GLOBALS[‘request’]->server->get(‘SCRIPT_NAME’) ) { $GLOBALS[‘request’]->server->set(‘SCRIPT_NAME’, ‘/index.php’); }

2. Create a .htaccess file on the /public_html folder with:

<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect to the subdirectory because that’s where Drupal is installed
RewriteRule (.*) web/$1 [L]

3. Update .htaccess under /public_html/web folder

Uncomment the line RewriteBase and set it to:

RewriteBase /web

4. Clear the cache and run update.php

Your site should work by browsing now [ i.e. without the /web suffix ]. Your menu items may still have the /web part, but it will be gone after some hard refresh.

5. If you want to redirect http/https and www/non-www:

On the .htaccess file under /public_html/web, please add those lines between the <IfModule mod_rewrite.c> tag

This is to redirect non-https + www to https + non-www:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*)$1 [L,R=301]

And this is to redirect non-https + non-www to https + www”

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]

Leave a Comment

Your email address will not be published. Required fields are marked *

Need Help?