Please examine and correct this code:

I want the code to work such that if somebody visits https://example.com/music/?record_year=2022, it should be https://example.com/music/record_year/2022 instead.

# To externally redirect https://domain.com/music/?record_year=2022 
RewriteCond %{THE_REQUEST} /music/?record_year=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ music/%1/%2/? [R=301,L,NE]
  • 2
    this can't be done via HTAccess. You could redirect to the desired URL ( redirects and rewrites arent the same thing ), but you'd need PHP code to allow your theme/plugin to retrieve the record year etc from the URL with custom WP rewrite rules ( HTAccess rewrite rules and WP rewrite rules aren't the same either )
    – Tom J Nowell
    Commented Jan 25, 2023 at 10:57
  • Am not sure about that, I read series of articles saying it is achievable through htaccess. Commented Jan 25, 2023 at 20:50
  • 1
    not in the way that you expect it to work, doing it properly requires rewrite rules as well as filters to make WordPress generate and use those new URLs, and if WP thinks it doesn't match the canonical URL it will redirect it to avoid SEO penalties. If you look you'll find plenty of ancient articles that say otherwise because they're very old or are flat out wrong
    – Tom J Nowell
    Commented Jan 25, 2023 at 21:57
  • Okay, great. What other means would you recommend please. Commented Jan 26, 2023 at 12:43


Your Answer

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

Browse other questions tagged or ask your own question.