0

I try to rewrite my URLs from domain.tld/?country=XX&admin1=YY&city=ZZ to domain.tld/XX/YY/ZZ

I have tried

RewriteEngine on
RewriteRule ^([A-Za-z]{2,2})/(.*)/(.*)$ /?country=$1&admin1=$2&city=$3 [QSA]

Unfortunately it doesn't work. Have I missed something?

1 Answer 1

1

You can have these rules in your root .htaccess:

RewriteEngine on

RewriteCond %{THE_REQUEST} /\?country=([^\s&]+)&admin1=([^\s&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]

RewriteRule ^(\w+)/(\w+)/(\w+)/?$ /?country=$1&admin1=$2&city=$3 [QSA,L]
Sign up to request clarification or add additional context in comments.

4 Comments

This worked for rewriting the URI. But now the site crashed because all included files like images are rewritten to domain.tld/XX/YY/some/image/source. Any suggestions?
That is due to use of relative paths for images/css/js. To fix you can add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's URL.
This worked for images (<img> elements), but not for javascript (<script> elements) and css (<link> elements). What about RewriteBase in htaccess?
No <base> tag affects css/js also. You are better off using absolute URL rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.