2

All my rewrite rules up to this moment used $1, $2... variables to form a clean URL.

Now I need to redirect basing on a part of domain name (http://subdir.example.com => http://example.com/subdir) and many examples I found using %1 variable instead of $1 format. I should note that subdir is an arbitrary string.

This is unclear for me. I'm trying to find the meaning of $1 and %1 variables for hour or two and still can not find anything useful.

Vlada Katlinskaya
  • 991
  • 1
  • 10
  • 26

1 Answers1

2

You can use this in your .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdir\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule (.*) /subdir/$1

Make sure you clear your cache before testing it.

A suggested edit from OP:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteCond %{REQUEST_URI} !^/????/
RewriteRule (.*) /????/$1
Joe
  • 4,877
  • 5
  • 30
  • 51
  • Could you make your code more general just in case `subdir` can be any arbitrary string? I think that it is the right place to use `%1`, which would be closer to the core of the question. Thanks! – Vlada Katlinskaya Jan 19 '17 at 07:41
  • I've included it as an edit, but not removed the original. Always good to keep both versions for people to test. However, I'm not 100% sure why you wanted `subdir` removed. This is their for people to replace, it has the same use as `example`. – Joe Jan 19 '17 at 08:47
  • Oh! You've included my question marks :))) I hoped that you will replace them with right code. Could you? – Vlada Katlinskaya Jan 19 '17 at 09:12
  • 2
    I came across this question: [Difference between $1 vs %1 in .htaccess.](http://stackoverflow.com/questions/6654834/difference-between-1-vs-1-in-htaccess) - I think this is what you're after. – Joe Jan 19 '17 at 10:09
  • 1
    Thank you! This is what I'm looking for. I think that these questions should be linked together in a style "*This question already answered here...*". Do you know how to do it? – Vlada Katlinskaya Jan 19 '17 at 10:18
  • 1
    Flagged as duplicate :) – Joe Jan 19 '17 at 10:26