如何使用mod_rewrite重写特定的URL?

时间:2019-04-04 14:32:37

标签: regex apache mod-rewrite url-rewriting

我有一个名为{HTTP:CF-IPCountry}的geoip http标头,用于存储国家代码,例如FR,US等。我需要做的就是将查询字符串loc = {HTTP:CF-IPCountry}添加到网站首页https://example.com/test-website/以及具有url以https://example.com/test-website/products/https://example.com/test-website/shop/开头的任何页面,如果该URL上使用了其他查询字符串,则附加查询字符串,以便用户请求该页面https://example.com/test-website/products/shoe/?size=s应该改写为https://example.com/test-website/products/shoe/?size=s&loc=us

这是htaccess文件的外观

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test-website/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test-website/index.php [L]

RewriteCond %{REQUEST_METHOD} !^POST
RewriteCond %{QUERY_STRING} !^loc [NC]
RewriteRule (.*) /%{REQUEST_URI}/?loc=%{HTTP:CF-IPCountry} [NC,NE,R,L,QSA]
</IfModule>

# END WordPress

我目前遇到的问题是,重写规则仅适用于网站首页https://example.com/test-website/和网站管理面板页面 https://example.com/test-website/wp-admin,但不适用于https://example.com/test-website/products/

之类的前端其他任何页面

希望您能尽可能简单地回答我的问题,尤其是我对rewriteRules没有任何经验。

谢谢

0 个答案:

没有答案
相关问题