Assuming that my website homepage is https://example.com/test-website/. What I want to do is to add a query string at the end of the homepage URL and on all pages that start with https://example.com/test-website/products/ and https://example.com/test-website/shop/.

This is my current code:

# 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 %{QUERY_STRING} !^loc [NC]
RewriteRule (.*) /%{REQUEST_URI}/?loc=%{HTTP:CF-IPCountry} [NC,NE,R,L,QSA]

# END WordPress

My code works fine and adds the 'loc' query string parameter to the homepage URL and to all pages on wp-admin but it doesn't work on any other pages on the frontend like https://example.com/test-website/products/. The page just opens normally but without the query string.

I tried to remove the following from my code

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test-website/index.php [L]

This resulted in making the homepage and wp-admin to still work fine but any other pages on the frontend to give 404 error.

Hope you can help me solve this issue, especially that I don't have experience in regex or mod_rewrite.

  • To clarify, your WordPress URLs actually contain the /test-website URL-path segment? Is your .htaccess file in the document root or in the /test-website directory? – MrWhite Apr 4 '19 at 21:54
  • @MrWhite all the files are inside /test-website directory – Islam Mohamed Apr 4 '19 at 22:40

What you are describing is an (external) redirect, not an (internal) "rewrite" as stated in your question title. (If you wanted a "rewrite" then you would not use .htaccess for this on a WordPress site.)

Also, you shouldn't be modifying the WordPress directives inside the # BEGIN WordPress code block - this is managed by WordPress and is likely to be overridden when WP updates (or at least it will try to).

I tried to remove the following from my code

The code you are trying to remove is part of the WordPress front-controller. If you remove that then pretty permalinks will not work and your site will likely break. (ie. Pages in the frontend won't resolve and you'll get a 404.)

You've basically put your redirect directives in the wrong place. By placing them after the WP front-controller they simply won't get processed for any "frontend" URLs (except for the homepage, which isn't caught by the preceding front-controller on the first pass). They need to go before the # BEGIN WordPress block near the top of your .htaccess file.

However, your directives are not specifically checking the URL-paths you want to redirect. ie. the homepage, /test-website/products/ and /test-website/shop/. Your generic regex .* matches everything!?

Assuming your .htaccess is located in the /test-website subdirectory then try the following instead:

RewriteCond %{QUERY_STRING} !^loc [NC]
RewriteRule ^($|products/|shop/) /%{REQUEST_URI}/?loc=%{HTTP:CF-IPCountry} [NC,NE,R,L,QSA]

# BEGIN WordPress

Aside: Do you really want to unconditionally append a trailing slash to the URL-path?

| improve this answer | |
  • Thanks so much for your answer, it seems to be working perfectly, I have one more question please which i hope you can help me with. I was wondering how to rewite the url without redirections to be https://example.com/test-website/%{REQUEST_URI}/ and https://example.com/test-website/%{REQUEST_URI}/shop/ i tried to edit the rule and just remove '?loc=' but i ended up having a redirection loop and url looked like https://example.com/test-website/EG/EG/EG/EG/... and i guess if i solved the loop issue will get a 404 error – Islam Mohamed Apr 4 '19 at 22:25
  • I know its much easier to do that using php and create a wordpress custom taxonomy but my original goal is to display different woocommerce pricing based on country and avoid the prices to be cached by page caching that's why i used mod_rewrite, so i'm wondering how to achive the same thing that can be done from wp permalink settings using htaccess sorry for the typo on the prevous comment, i meant the link to be example.com/test-website/%{HTTP:CF-IPCountry}/ and example.com/test-website/%{HTTP:CF-IPCountry}/shop/ – Islam Mohamed Apr 4 '19 at 22:34
  • It's difficult to rewrite the URL in .htaccess when using WordPress, because of the way WordPress routes URLs (it looks at the requested URL, not at the rewritten URL). You would need to do this in WordPress itself. However, if the information is being sent in an HTTP request header then there is really no need to rewrite (or redirect for that matter). Why not just access the CF-IPCountry HTTP request header directly in PHP? – MrWhite Apr 4 '19 at 23:38
  • If my answer above answered your initial question then please mark it as accepted (checkmark below the voting arrows on the left) to remove it from the unanswered question queue. Once you have 15+ rep then you can also upvote answers you find useful. Thanks, much appreciated. :) – MrWhite Apr 5 '19 at 1:09
  • Thanks so much MrWhite, Sure i accepted your answer wished i can upvote it too :) The reason i didn't use php to access CF-IPCountry and used rewrite instead is to avoid page caching, i have woocommerce store with prices varies depend on country so if the page got cached when a us visitor visited it all links on the page for uk user will have /us/ on the link. however i though now for a wayy too do it... to add a custom taxonomy that use CF-IPCountry in php to create the dynamic taxonomy/directories and then use rewite to redirect to those links generated by wordpress. – Islam Mohamed Apr 5 '19 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.