.htaccess 按n个字符拆分url

.htaccess 按n个字符拆分url,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有一个网站流量太大,我想缓存一些页面,这应该可以缓解问题 我已经有了一个这样的系统,但问题是url结构会导致11365万页保存在一个目录中,例如 dir/*尝试一下以下规则: RewriteEngine On RewriteRule ^(people/.*?)([^/]{3})([^/]+)$ /$1$2/$3 [R=302,L] RewriteCond %{DOCUMENT_ROOT}/caches/html%{REQUEST_URI}.html -f RewriteRule ^ %{D

我有一个网站流量太大,我想缓存一些页面,这应该可以缓解问题

我已经有了一个这样的系统,但问题是url结构会导致11365万页保存在一个目录中,例如


dir/*尝试一下以下规则:

RewriteEngine On

RewriteRule ^(people/.*?)([^/]{3})([^/]+)$ /$1$2/$3 [R=302,L]

RewriteCond %{DOCUMENT_ROOT}/caches/html%{REQUEST_URI}.html -f
RewriteRule ^ %{DOCUMENT_ROOT}/caches/html%{REQUEST_URI}.html [L]

OP建议的编辑在中被拒绝。以下是OP的解决方案:

# Set an environmental var for the root directory, so it works on local dev and live servers

RewriteCond %{SCRIPT_FILENAME} ^(.+)\/index.php$    [NC]
RewriteRule .* - [E=PATH:%1]

# Pick up the actual request from query string and set it as an environmental var

RewriteCond %{QUERY_STRING} ^request=names\/(.*?)([^/]{3})([^/]+) [NC]
RewriteRule .* - [E=SN:%1%2/%3]

# If a cache paged exists, internal redirect to that

RewriteCond %{ENV:PATH}/cache/html/names/%{ENV:SN}.html -f
RewriteRule .* cache/html/names/%{ENV:SN}.html [L]

# Send requests that are not cached to php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^.+$ index.php?request=$0 [QSA,L]

如果名称正好是n个字符的倍数呢?在我将预测搜索缓存到json文件中时,它的工作原理如下:1)查询“a”将生成/cache/a.json;“an”=>/cache/an.json;和“=>/cache/and.json;“andr”=>/cache/and/r.json。。。等等虽然mod_rewrite我也希望这样做,但我认为这是不可能的,它必须通过PHP路由来拆分URL?这不是我最初想要的拆分类型,但经过一些思考后,我认为它实际上会更好。我已经编辑并添加了在我的特定示例中起作用的代码。
RewriteEngine On

RewriteRule ^(people/.*?)([^/]{3})([^/]+)$ /$1$2/$3 [R=302,L]

RewriteCond %{DOCUMENT_ROOT}/caches/html%{REQUEST_URI}.html -f
RewriteRule ^ %{DOCUMENT_ROOT}/caches/html%{REQUEST_URI}.html [L]
# Set an environmental var for the root directory, so it works on local dev and live servers

RewriteCond %{SCRIPT_FILENAME} ^(.+)\/index.php$    [NC]
RewriteRule .* - [E=PATH:%1]

# Pick up the actual request from query string and set it as an environmental var

RewriteCond %{QUERY_STRING} ^request=names\/(.*?)([^/]{3})([^/]+) [NC]
RewriteRule .* - [E=SN:%1%2/%3]

# If a cache paged exists, internal redirect to that

RewriteCond %{ENV:PATH}/cache/html/names/%{ENV:SN}.html -f
RewriteRule .* cache/html/names/%{ENV:SN}.html [L]

# Send requests that are not cached to php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^.+$ index.php?request=$0 [QSA,L]