Using Rewritebase in .htaccess

ぃ、小莉子 提交于 2021-01-29 22:02:11

问题


I'm using a .htaccess to redirect all calls to http://domain/site1/api/ to index.php in /site1/api/ folder. This index.php is the entry point for a rest api.

# /site1/api/.htaccess
RewriteEngine On
RewriteBase /site1/api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

It works fine, but is there a better way than use a hard coded RewriteBase? I would like to this two urls work: http://site1.domain/api/ and http://domain/site1/api/.


回答1:


You can sort of create a dynamic base using environment variables. So at the very top of your htaccess, maybe something like this:

RewriteCond %{ENV:BASE} ^$
RewriteCond $1::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^(.*)$ - [ENV=BASE:%2]

So the "BASE" variable becomes /site1/api/. And you'd then use it like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:BASE}/index.php [QSA,L]


来源:https://stackoverflow.com/questions/36085003/using-rewritebase-in-htaccess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!