Skip to main content
edited title
Link
Brad Bell
  • 67.6k
  • 6
  • 75
  • 145

htaccess.htaccess for craftcmsCraft CMS - removing segment from URL

Source Link

htaccess for craftcms - removing segment from URL

I'm rebuilding a website which needs to run Craft CMS as the main frontend and a bespoke CRM system at the same domain.

The file setup is basically

  • public_html
  • cms
  • crm

To make things a bit more complicated, certain parts of the CRM have to be publically accessible, so there are a few different URL structures that need to run from the CRM, and the rest from Craft. Also, the images/scripts are in the cms folder, so I need to get those to work without cms in the URL.

For the most part this is working lovely, but I can't get force /cms/ to be removed from the URL, so whilst the site works fine without it, it's live on 2 different URLs.

I have the with the following code:

<IfModule mod_rewrite.c>

    # Force the root to match slug of home page
    # This is the only way I can get the home page to
    # show on the root, as it's within a structure
    RewriteRule ^$ /__home__

    # Get rid of /cms (this doesn't work)
    RewriteRule ^cms/?(.+)$ /$1 [R=301]

    # Sort out frontend files
    RewriteCond %{REQUEST_URI} /assets/.*
    RewriteCond %{REQUEST_URI} !/assets/admin/.*
    RewriteCond %{REQUEST_URI} !/cms/.*
    RewriteCond %{REQUEST_URI} !/crm/web/.*
    RewriteRule ^(.*\.(gif|jpg|jpeg|bmp|svg|png|css|js|json|pdf|eot|woff|woff2|ttf|otf))$ /cms/$1 [QSA,L]
    
    # Sort out admin files
    RewriteCond %{REQUEST_URI} /assets/admin/.*
    RewriteCond %{REQUEST_URI} !/cms/.*
    RewriteCond %{REQUEST_URI} !/crm/web/.*
    RewriteRule ^(.*\.(gif|jpg|jpeg|bmp|svg|png|css|js|json|pdf|eot|woff|woff2|ttf|otf))$ /crm/web/$1 [QSA,L]

    # Send would-be 404 requests to Craft
    # Need to exclude any that should route to the CRM
    # Any that we exclude, we then need to route below
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteCond %{REQUEST_URI} !^/manager/?(.*)$ [NC]
    RewriteCond %{REQUEST_URI} !^/forms/(.+)$ [NC]
    RewriteCond %{REQUEST_URI} !^/partners/?(.*)$ [NC]
    RewriteCond %{REQUEST_URI} !^$ [NC]
    RewriteRule (.+) /cms/index.php/$1 [QSA,L]

    # CRM Admin
    RewriteCond %{REQUEST_URI} ^/manager/?(.*)$ [NC]
    RewriteRule ^manager/?(.*) /crm/web/app.php [QSA,L]
    
    # Quote Forms
    RewriteCond %{REQUEST_URI} ^/forms/(.+)$ [NC]
    RewriteRule ^forms/(.+) /crm/web/app.php [QSA,L]
    
    # Signup Forms
    RewriteCond %{REQUEST_URI} ^/partners/?(.*)$ [NC]
    RewriteRule ^partners/?(.*) /crm/web/app.php [QSA,L]
    
</IfModule>

So I'm fairly sure that the line RewriteRule ^cms/?(.+)$ /$1 [R=301] would work, but the default Craft logic is somehow preventing it - if I comment out the default craft section, then it removes cms from the URLs, but obviously nothing is routed through Craft, so 404s all round.

I'm completely stumped at this point, so any help would be appreciated!