How to rewrite specific urls using mod_rewrite?

社会主义新天地 提交于 2019-12-17 20:43:29

问题


I have a geoip http header named {HTTP:CF-IPCountry} that store the country code like FR, US etc.. what i need to do is to add this query string loc={HTTP:CF-IPCountry} to the website homepage https://example.com/test-website/ and to any page that has url start with https://example.com/test-website/products/ or https://example.com/test-website/shop/ and append the query string if there are any other query strings used on the url so if the user requested page https://example.com/test-website/products/shoe/?size=s it should rewrited as https://example.com/test-website/products/shoe/?size=s&loc=us

This is how htaccess file looks like curently

# 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

The issue i have at the moment is that the rewrite rule works only on the website homepage https://example.com/test-website/ and the website admin panel pages https://example.com/test-website/wp-admin but it doesn't work with any other pages on the frontend like https://example.com/test-website/products/

Hope you can answer my question as simply as posible especially that i dont have any experiance with rewriteRules.

Thanks

来源:https://stackoverflow.com/questions/55518589/how-to-rewrite-specific-urls-using-mod-rewrite

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