Home > database >  Htaccess - manage catch all in multiple directories and one single htaccess file
Htaccess - manage catch all in multiple directories and one single htaccess file

Time:08-01

I'm trying to dispatch all traffic in multiple directories based on a specific keyword.

I have the following directory structure:

dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)

I have a .htaccess file located in dotcom and I would like to redirect everything behind each directory to an index file in each directory.

Example:

dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php

Anything not in one of the existing directories should be redirected to dotcom/index.php

I tried the following for dotcom:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This catches everything

But when I tried to add conditions like the following, I get a 404:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]

With this, if I try to access dotcom/directory1/blabla I have a 404 while if I access dotcom/directory1/ it goes to the right index.php

I have tried to use the full path dotcom/directory1/ but it doesn't help.

CodePudding user response:

With your shown samples, please try following htaccess rules file. Please make sure your .htaccess rules file and folder dotcom are residing in same folder.

RewriteEngine ON

RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(dotcom)/?$ $1/index.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(dotcom(?:/[^/] )*/?))$ $1/index.php [QSA,NC,L]

Please make sure to clear your browser cache before testing your URLs.

CodePudding user response:

I have found something that works with the following:

RewriteEngine ON

RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]

This way I'm catching everything from /directoryX/ and redirect it to the root of the directory, everything else go to dotcom

  • Related