.htaccess rewrite folder, and set environmental params
I would like to do a trick like this:
I have these folders
folder_dev
folder_live
and this code base:
folder
All f开发者_StackOverflow中文版olders are on the same level.
I would like to
- redirect
folder_dev
tofolder
and set an environmental param THEME to 'dev' - redirect
folder_live
to the samefolder
but set an environmental param THEME to 'live'
If you want to have the physical directories, you'll need a .htaccess file in each one, set up as follows:
In /folder/.htaccess:
SetEnvIf REDIRECT_THEME (.*) THEME=$1
In /folder_dev/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)(?:folder_dev)
RewriteRule .* %1folder/$0 [E=THEME:dev]
In /folder_live/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)(?:folder_live)
RewriteRule .* %1folder/$0 [E=THEME:live]
You need the SetEnvIf directive in the codebase folder because the environment variables set by mod_rewrite are always prefixed by REDIRECT_, which necessitates a final translation from REDIRECT_THEME to THEME.
You could also likely consolidate them into a single .htaccess file in the directory above (the fourth level in this case), allowing you to get rid of the folder_dev and folder_live directories if you didn't need them for another reason:
SetEnvIf REDIRECT_THEME (.*) THEME=$1
RewriteEngine On
RewriteRule ^folder_dev/(.*)$ folder/$1 [E=THEME:dev]
RewriteRule ^folder_live/(.*)$ folder/$1 [E=THEME:live]
# Alternatively just:
# RewriteRule ^folder_([^/]+)/(.*)$ folder/$2 [E=THEME:$2]
精彩评论