规范的重写规则定制

rewriterule customization for canonical

这是我现在在我的 htaccess 文件中使用的代码。

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|webp|html)(\.gz)?(\?.*)?$"> 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule .* - [E=CANONICAL:http://%{HTTP_HOST}%{REQUEST_URI},NE]
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=CANONICAL:https://%{HTTP_HOST}%{REQUEST_URI},NE]
</IfModule>
<IfModule mod_headers.c> 
Header set Link "<%{CANONICAL}e>; rel=\"canonical\"" 
</IfModule> 
</FilesMatch>

我想要做的是让这里的规范不同于 HTTP_HOST。

我们只是说 HTTP_HOST 现在是示例。com/es/ 但我需要这个规范是示例。com/en/

我如何使用上面的代码进行修改来重写规范的 URL 以反映示例。com/en/

试试这个:

<IfModule mod_rewrite.c> 
RewriteEngine On 

RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(.*?)/es/(.*)$ [NC]
RewriteRule .* - [E=CANONICAL:%1/en/%2,NE,S=1]

RewriteCond %{HTTPS}s ^on(s)|off
RewriteRule .* - [E=CANONICAL:http%1://%{HTTP_HOST}%{REQUEST_URI},NE]
</IfModule>