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 

1 Answer 1

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

3
  • 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. Commented May 17, 2018 at 12:00
  • @MrWhite Quite correct, I never use .htaccess. But isn't the ".*" greedy enough to capture the entire URL now? Commented May 17, 2018 at 13:03
  • "But isn't the ".*" greedy enough to capture the entire URL now?" - yes, now that the slash has been removed. Commented May 17, 2018 at 13:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.