0

Looking for help in writing an .htaccess file to redirect to a new address with the url/query string intact --

User url input:

http://www.example.com/path?query-string=something

   -or-

http://example.com/path?query-string=something

   will be redirected to:

http://subdir.example.com/path?query-string=something
Mike B
  • 9
  • 1

1 Answers1

2

You don't need to worry about the query string.

RewriteCond %{HTTP_HOST} example.com$
RewriteRule (.*)$ http://subdir.example.com/$1 [R]

This will only work with the .htaccess in the root

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11
  • You'll need a start-of-string anchor (`^`) to catch the whole URL-path (and not just the last path segment). But in a `.htaccess` context the URL-path matched by the `RewriteRule` _pattern_ does not start with a slash. You'll also need a _condition_ to check the `Host` header if the subdomain points to the same area on the filesystem, to prevent a redirect loop. – MrWhite May 17 '18 at 12:00
  • @MrWhite Quite correct, I never use .htaccess. But isn't the ".*" greedy enough to capture the entire URL now? – Gerard H. Pille May 17 '18 at 13:03
  • "But isn't the ".*" greedy enough to capture the entire URL now?" - yes, now that the slash has been removed. – MrWhite May 17 '18 at 13:57