I have moved pages in various sub directories to the root directory and want to set up a 301 redirect for each page and keep any query string that might be present. For example:

  • www.example.com/folder/page1.html will redirect to: www.example.com/page1.html


  • www.example.com/folder2/page2.html will redirect to: www.example.com/page2.html

If a query string is present, which we sometimes use, I would like that preserved. There are multiple pages which were in multiple subfolders that I need to redirect. All could potentially have query strings.

I've done simple redirects for landing pages where the landing page has a definitive query string and can hard code the query string since it will always be the same. That's not the case here.

I did find the following example that is close, but don't understand enough of it to know if it will work, and there happens to be ONE subfolder whose contents aren't being moved, so...

RewriteEngine On
RewriteRule ^subfolder/(.*)$ /$1 [R=301,QSA,L] 
  • Also, as an additional example with a query string: www.example.com/folder1/page1.html?m=3 will redirect to www.example.com/page1.html?m=3.
    – J Green
    Commented Oct 31, 2018 at 20:03
  • Are you wanting to redirect everything within a subfolder or just certain files within that subfolder? Do you have any existing directives in your .htaccess file?
    – MrWhite
    Commented Oct 31, 2018 at 20:25
  • Everything within the subfolder. I only have one subfolder containing .html files that I do NOT want to redirect. All the other pages in their subfolders need to be redirected.
    – J Green
    Commented Oct 31, 2018 at 20:30

1 Answer 1

RewriteRule ^subfolder/(.*)$ /$1 [R=301,QSA,L]

This would work just fine to redirect all files within the specific subfolder. The QSA flag is not required as the query string is preserved by default. The QSA flag would only be required if you included a query string on the substitution (ie. the 2nd arg to the RewriteRule directive - the target URL) and you wanted the original query on the request to be appended (Query String Append).

However, if you want to redirect every subdirectory except for one then you can do this in a single rule by creating a exception for the subdirectory you don't want redirected.

For example, using mod_rewrite in .htaccess:

RewriteCond %{REQUEST_URI} !^/subdir-not-redirected/ [NC]
RewriteRule ^[^/]+/([^/]*)$ /$1 [R=302,L]

If it is only .html files you want to redirect then be more specific with the regex...

RewriteRule ^[^/]+/([^/]*\.html)$ /$1 [R=302,L]

This only matches files in a single subdirectory. It won't match files that are more than one subdirectory deep.

This is a 302 (temporary) redirect. Change to a 301 (permanent) only when you have confirmed that it's working OK - to avoid potential caching issues whilst testing.

Just as an alternative, if you don't have any existing mod_rewrite directives, then you can also do this with a single mod_alias RedirectMatch directive instead:

RedirectMatch 302 ^/(?!subdir-not-redirected)[^/]+/([^/]*\.html)$ /$1

This uses a negative lookahead to create an exception for the subdirectory you don't want redirected. (You could use the same method with the above RewriteRule, instead of using a separate RewriteCond directive.)

  • Thank you so much for replying. I will give this a shot and let you know how it went!
    – J Green
    Commented Oct 31, 2018 at 21:39
  • Hi, thank you for the answer. I do have other mod_rewrites (to direct all traffic to the https: version of the site. I placed this condition and rule below those in the .htaccess file and I'm getting a 503 Service Temporarily Unavailable error when testing one of the old url paths.
    – J Green
    Commented Nov 1, 2018 at 14:08
  • Since there were only about 20 urls needing to move, I used the following for each one, for now: RewriteRule ^/folder/page.html https: //example.com/page.html [301,L]
    – J Green
    Commented Nov 1, 2018 at 15:20

Your Answer

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

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