0

I try to understand how to manage subdomains in Apache (mod_rewrite module).
On this website:
http://htaccess.madewithlove.be/

This link below:

http://www.sub.tst.com/foo/bar.php  

Under this rule:

RewriteRule (.*)bar(.*)$ $1oxo$2 [PT]

Returns:

✔ The new url is http://www.sub.tst.com/foo/oxo.php

But under following rule (or any other similar):

RewriteRule (.*)tst(.*)$ $1oxo$2 [PT]

It returns an error:

✖ This rule was not met.

I want to redirect links with subdomains to proper subfolders (to tst.com/sub/foo/bar link - if it exists), so I think I just have to cut www.sub.tst.com/foo/bar.php into 2 chunks, where 'sub' and '/foo/bar.php' I put after 'tst.com' (and remove '.php' extension from the end of it), but so far regex is not working on rewrite rules as I expect (and as I test it on php).
What is wrong? How to get it right? How to test it, before I use it on my website?

cooba
  • 3
  • 2

1 Answers1

1

The search pattern uses standard regular expressions. So in your case, the parenthesis constitutes a regex group. The first one matched will take the place of $1, and so on. Remember also that regex are greedy. That is they will match as much as they can.

The reason your RewriteRule doesn't match your example, is because the PATTERN is only checked against "[...] the part of the URL after the hostname and port".

To accomplish what you want, you might want to use a combination of RewriteCond with RewriteRule. So basically use RewriteCond to determine if a subdomain matches, then rewrite the domain part entirely. Example:

RewriteCond %{HTTP_HOST} ^www.sub.tst.com [NC]
RewriteRule ^(.*)$ http://tst.com/sub/$1 [L]

In this case, the RewriteCond works as an if statement for the following RewriteRule.

It's not pretty, but it might do the work for you.


Updated: based on comments from cooba and olaf.

You can accomplish what you need with the following rules

RewriteCond %{HTTP_HOST} !www.tst.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)*(([^.]*).)tst.com [NC]
RewriteRule ^(.*)$ http://tst.com/%3/$1 [L]

Start from the 2nd RewriteCond. Here, the first part of of the regex ^(www.) is needed to find cases where the domain contains www or not (www.sub.tst.com or sub.tst.com).

The second part of the regex (([^.]*)\.) matches the subdomain. The part we want is the inside group (the one without the .). When the regex is evaluated, that inner group takes position 3. To access that value in the RewriteRule, you use %.

Now, the 1st RewriteCond is needed to guard about rewriting www.tst.com into tst.com/www/.

Community
  • 1
  • 1
jzer7
  • 171
  • 4
  • If I use the same pattern for http://sub.tst.com/foo/bar link - than "This condition is not met". There are so many cases and subfolders, so do I have to create always a separate rule for each one of them? – cooba May 22 '16 at 09:28
  • 1
    @cooba You can capture part of the domain, e.g. `www.(.+?).tst.com` and then use it in the substitution `http://tst.com/%1/$1`. Notice the percent `%` vs dollar `$` sign. – Olaf Dietsche May 22 '16 at 14:33
  • @OlafDietsche Your regex is still catching only one case: [https://regex101.com/r/xZ6uK1/1](https://regex101.com/r/xZ6uK1/1). Why you use single dot '.' instead of '\.'? Do you know any other place where I could ask about RewriteRules? – cooba May 22 '16 at 14:58
  • @cooba Most of your samples are irrelevant, because `HTTP_HOST` is just the domain part, nothing else. It doesn't include `http`, `/`, or any other part of the request. To learn about mod_rewrite, you can just follow jzer7's link. But in this case, it's more about the regular expression. – Olaf Dietsche May 22 '16 at 15:04
  • Using `.` instead of `\.` was just laziness. – Olaf Dietsche May 22 '16 at 15:06
  • Thank you @olafDietsche, that tip about `%1` was great. – jzer7 May 22 '16 at 15:22
  • @jzer7 On my website last rules are creating deeper cascading loop (from: /foo/bar.php - towards: /foo/foo/foo/bar.php...) and it's opening index.php file - instead of bar.php file. What could be wrong? Is there any non site-affecting way to test htaccess file custom modifications? – cooba May 28 '16 at 22:27