Rewrite Condition to compare top level directory from Request_URI

前端 未结 1 1225
滥情空心
滥情空心 2021-01-25 14:32

I have several clients on my website with largely similar sites setup for each client. The folders for these client accounts are set up for organizational purposes at:

1条回答
  •  被撕碎了的回忆
    2021-01-25 15:29

    RewriteCond can have a part of the match as its first argument, so you can use

    RewriteCond %{DOCUMENT_ROOT}/client/$1 -d
    RewriteRule ^(.+?)/(.*)$ /client/$1/$2 [L,QSA]
    

    Apache will check if %{REQUEST_URI} starts (^) with a directory name of nonzero length followed by a slash, then it will use the first match to see if a directory of that name exists witih %{DOCUMENT_ROOT}/client/. If so, the rewrite rule will apply whatever follows the slash (even nothing).

    E.g. if the user requests the page

    mydomain.com/sampleclient/orders
    

    Apache will check if %{DOCUMENT_ROOT}/client/sampleclient exists and is a directory, and if so, it will load

    mydomain.com/client/sampleclient/orders
    

    Generally, it will load mydomain.com/client/sampleclient/[whatever comes after the first slash].

    If you don't need it to load mydomain.com/client/sampleclient/orders and want to apply the rewrite rules inside the sampleclient directory, use

    RewriteRule ^(.+?)/(.*)$ /client/$1/ [QSA]
    

    That is, remove the $2 from the replace string, and the L flag inside square brackets.

    See also these Apache docs for more info.

    0 讨论(0)
提交回复
热议问题