2

I tried the following code in .htaccess to 301 redirect www.example.com/?content=file.php&id=16 www.example.com/file/This-is-the-title/16

RewriteEngine on
Redirect 301 /?content=file.php&id=16 /file/This-is-the-title/16

But it's not redirecting. The URL remains as it is.

What am I doing wrong?

P.S. I'm not asking for rewrite or so. I need a 301 redirect.

  • Did my answer work for you? Perhaps you could accept it? Thanks. –  Feb 16 '17 at 05:05
  • I removed your edits David, other than the typo on the replacement URL which was taken from your question (and is still there - I will update your question). To go through them, the REQUEST_URI can be tested in the RewriteRule, ^$ is the same as matching `/` when it's in .htaccess. The query_string can be matched the way I am doing it, it does not have to be a regex. The base URL `http://www.example.com` does not have to be in the replacement when it is not changing. –  Feb 16 '17 at 14:09
  • I also put back `RewriteEngine on` as it is needed, so useful for anyone referencing this later, even if you happen to already have that in place for other rules, so don't need it against this one. It's to make the answer complete. –  Feb 16 '17 at 14:18

1 Answers1

2

The Redirect directive doesn't match query strings. Use this instead:

RewriteEngine on
RewriteCond %{QUERY_STRING} =content=file.php&id=16
RewriteRule ^$ /file/This-is-the-title/16? [R=301,L]
  • 1
    You'll just need a single `?` on the end of the `RewriteRule` _substitution_ (or `QSD` flag on Apache 2.4+) to remove the query string from the redirected URL. – MrWhite Feb 12 '17 at 23:31
  • Thanks, I always forget that for these! Perhaps I'll remember now :) –  Feb 12 '17 at 23:31