I have the following folder structure
ROOT (loaded on domain.com)
campaigns
| campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
At the moment to access the website inside the folder campaign1
i would have to enter in the URL address bar: domain.com/campaigns/campaign1
What should i put in the .htaccess
file so that when you put
domain.com/campaign1
the browser loads and shows everything from domain.com/campaigns/campaign1
but of course without visibly changing the URL in the address bar.
Thank you so much for your help.
CodePudding user response:
You could do something like the following in the root .htaccess
file using mod_rewrite. This is a general version if the "campaign" subdirectories are not known (or too many):
RewriteEngine On
# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/$1 -d
RewriteRule ^([^/] )/ campaigns%{REQUEST_URI} [L]
Where $1
is a backreference to the captured subgroup in the RewriteRule
pattern.
i would have to enter in the URL address bar:
example.com/campaigns/campaign1
Since campaign1
is a directory you should be requesting campaign1/
(with a trailing slash) otherwise mod_dir is going to issue an external redirect to append the trailing slash.
when you put
example.com/campaign1
the browser loads and shows everything from ...
Likewise, you should be requesting example.com/compaign1/
- with a trailing slash - and the above rule assumes that you are. It won't do anything if the trailing slash is omitted and you will get a 404. (If you are expecting third party requests where the trailing slash is omitted then you will need to manually issue a 301 redirect to append the trailing slash before the above rule.)
UPDATE:
What if i know the name of the campaign folder that goes into the campaigns folder? basically if i want to manually add the rule but campaign specific?
Yes, you can do this. In fact, if the number of campaigns you wish to rewrite in this way is limited then this may even be preferable since it avoids the filesystem check.
You may also be able to remove the second rule (second part of section#1 above) that prevents further processing should a file or directory be requested. ie. Remove the following:
RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L]
So, instead of (or before) section#2 above you could do the following...
For a single campaign:
# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]
For multiple campaigns:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 =campaign1 [OR]
RewriteCond $1 =campaign2 [OR]
RewriteCond $1 =campaign3
RewriteRule ^([^/] )/ campaigns%{REQUEST_URI} [L]
Note that it is specifically $1 =campaign1
, the =
is a prefix-operator on the CondPattern (2nd argument) itself. The =
operator makes it an exact string match, not a regex.
Or, using a regex and alternation:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/] )/ campaigns%{REQUEST_URI} [L]
(Although this could be combined into a single directive.)
UPDATE#2:
Add this before the internal rewrites to add a trailing slash at the end of the URL:
RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
Note that all internal links must already linking to the URL with a trailing slash. This redirect is only for the benefit of search engines and third party links that might be referencing the non-canonical URL without a trailing slash.