开发者

.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

  1. redirect folder_dev to folder and set an environmental param THEME to 'dev'
  2. redirect folder_live to the same folder 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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜