"rewrite" structure for subdomain

Discussion in 'Installation/Configuration' started by Emsanator, Dec 25, 2021.

  1. Emsanator

    Emsanator Member

    Hello,
    I want to do a custom "rewrite" for the subdomain. When I add the code below for this, the subdomain is redirected to the main domain.

    Sample #1:

    Code:
    server {
    if ($http_host = "panel.example.com") {
                 rewrite ^(?!/(_SubDomains/Panel|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Panel/$2 last;
                 rewrite ^/(.*)/(.*)$ /index.php?cmd=$1&scd=$2? last;
             }
    }
    If I do as in example two, Nginx server does not work.

    Sample #2:

    Code:
    if ($http_host = "panel.example.com") {
                 rewrite ^(?!/(_SubDomains/Panel|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Panel/$2 last;
                 location ~ \.php$ {
                              rewrite ^/(.*)/(.*)$ /index.php?cmd=$1&scd=$2? last;
                 }
             }
    How can I do subdomain-specific rewrite?
     
  2. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    How did you create the subdomain in the first place? In ISPConfig we can create it as a website totally on its own, or sub domain to main domain website without own vhost, or with its own vhost.
     
  3. Emsanator

    Emsanator Member

    Hi,

    "sub domain to main domain website without own vhost" - How?

    First I created "Client". Then I created the main domain based on 'client'. After creating the main domain, I created the subdomain.

    Client:
    upload_2021-12-25_13-59-35.png

    Website:
    upload_2021-12-25_13-58-30.png

    Sub Domain:
    upload_2021-12-25_14-1-13.png
    Sub Domain Detail:
    upload_2021-12-25_13-55-59.png
     

    Attached Files:

  4. Jesse Norell

    Jesse Norell ISPConfig Developer Staff Member ISPConfig Developer

    There is a setting to enable in main config, I don't remember the exact name, but it was posted within the last week or so.
     
  5. Emsanator

    Emsanator Member

    I assume you are talking about these options. I activated it first option.
    upload_2021-12-25_19-30-12.png

    So can this be a solution for rewrite?
    I'm trying to use it on Nginx server, as I use it on Apache server. I'm trying to transfer from CWP to ISPconfig, but this is the only hurdle in front of me.
    Code:
    RewriteRule ^([^/]+)/([^/]+)?$ index.php?cmd=$1&scd=$2 [L,QSA]
    RewriteRule ^([^/]+)/?$ index.php?cmd=$1 [L,QSA]
    Can you help with this?
     
  6. Steini86

    Steini86 Active Member

    I am not sure if I understand what you are trying to achieve. Can you give an example:
    What (domain) should the user type in and where should he be redirected?
     
  7. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    I think the easiest way out for you should be to transfer your old server to ISPConfig with apache2 so it will work straight away.

    You can learn ISPConfig with nginx if you got extra setup and time, so you don't have to rush things out.

    As I mentioned earlier the rewrite you want to achieve depends very much on your subdomain setup and your target since each has its own advantages, pros and cons.
     
  8. Emsanator

    Emsanator Member

    While using the Apache server, I was using a structure like below. This structure made, the anchor URLs, look more "friendly". Unfortunately, I cannot do the same with Nginx.

    ".htaccess" file for Apache;
    Code:
    <IfModule mod_rewrite.c>
        SetEnv HTTP_MOD_REWRITE On
        RewriteEngine on
        RewriteBase /
        RewriteCond %{REQUEST_URI} !\.php$ [NC]
        RewriteCond %{REQUEST_URI} [^/]$
        RewriteRule ^(.*)$ $1.php [L]
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l
        RewriteCond %{REQUEST_URI} !.(css|gif|ico|jpg|js|png|swf|txt)$
        RewriteRule ^ - [L]
         RewriteRule ^([^/]+)/([^/]+)?$ index.php?cmd=$1&scd=$2 [L,QSA]
        RewriteRule ^([^/]+)/?$ index.php?cmd=$1 [L,QSA]
    </IfModule>
    Example links:
    Without rewrite;
    Code:
    https://panel.example.com/index.php?cmd=details&scd=advent10..
    With rewrite;
    Code:
    https://panel.example.com/details?scd=advent10..

    I can call the required pages with "CMD", and add the required arguments at the end. That's all.
    I was using a CWP host management panel before, and I wanted to stop using it for open source, fast, user community, etc. There were several options in my host management, and I preferred to use ISPconfig. I created a new virtual machine and transferred all files with RSYNC. I prepared everything, but I ran into this problem. Furthermore, I don't plan to use ISPconfig 2 because I intended to leave the Apache server and go with the Nginx server entirely, but I've been unsuccessful in this issue for now.
    I thought there were a lot of ISPconfig users, but I don't know if they are not used much or if they are at a level to be called "dead" anymore. I opened many questions on Stack Overflow but got no answers. I'll have to revert to CWP, albeit unintentionally.
     
  9. till

    till Super Moderator Staff Member ISPConfig Developer

    Your problem is not ISPconfig related at all, you decided to use Nginx and not Apache and Nginx has a different rewrite syntax, that's all. And you can basically do any rewrite that's possible in Apache with Nginx as well. But if you don't have experience with nginx and are not willing to learn its syntax, then you should reinstall the server with ISPConfig 3 and Apache. Or use a .htaccess to nginx syntax converter. e.g. as a starting point:

    https://winginx.com/en/htaccess
    https://www.getpagespeed.com/apache-to-nginx
    https://timmehosting.de/htaccess-converter
    https://labs.gidix.de/nginx/
    https://www.nginx.com/blog/converting-apache-to-nginx-rewrite-rules/

    ISPconfig 2? ISPConfig 2 is EOL for probably 10 years, the current version of ISPConfig is ISPConfig 3.2 which supports Apache and Nginx web servers.

    Somewhere between half a million and a million servers worldwide, growing at a rate of about 1000 installations daily, so there are plenty of users ;) But we have christmas holidays as you might know and many of the ISPConfig users posting here are professional users that simply prefer to spend the holidays with their family instead of answering questions here at the forum.

    That's to be expected as Stackoverflow is not a community site of the ISPConfig community. The ISPConfig community is to be found mostly here at the Howtoforge ISPConfig forum.
     
    ahrasis and Steini86 like this.
  10. Emsanator

    Emsanator Member

    Of course, I don't think it has anything to do with ISPconfig. As I said before, it's an issue caused by Apache and Nginx server differences, and I'm trying to solve this problem. Actually, the issue is not big, I just don't have much experience with Nginx rewrites. This was incomplete in my experience because Apache and Nginx are currently running together on the server I'm actively using. Therefore, I did not have any advanced experience with Nginx. That's why I want help from your experience.
    --
    Some links on the site are sent with Ajax, and the ".php" extension is not available at the end of these links.
    E.g; https://panel.example.com/app/controller/ajax/collect
    At the same time, there is no ".php" suffix at the end of the site-wide pages.
    With Ajax, I am able to send data without the ".php" at the end. Like this;
    Code:
    try_files $uri/ $uri.html $uri.php$is_args$query_string;
    it works. However, rewrite does not work with the above code:
    Code:
    rewrite ^/([^/]+)/([^/]+)?$ /index.php?cmd=$1&scd=$2 last;
    rewrite ^/([^/]+)/?$ /index.php?cmd=$1 last;
    As a result, the above codes are correct and working but not working at the same time. How can I run these two codes at the same time?
     
  11. Steini86

    Steini86 Active Member

    Well, these mod_rewrites always involve a lot of testing. As I do not have a similar environment like yours I can not test it.

    Use a "break" statement (?) or "if"? Or a combination?
    Have you tried any of the links Till posted? For example the first one. If I put your htaccess code into the converter I get:
    Code:
    # nginx configuration by winginx.com
    
    location ~* \.php$ {
    }
    
    location ~ .(css|gif|ico|jpg|js|png|swf|txt)$ {
    }
    
    location ~ ^(.*)$ { }
    
    location / {
      rewrite [^/]$ /$1.php break;
      if (-e $request_filename){
        rewrite ^/([^/]+)/([^/]+)?$ /index.php?cmd=$1&scd=$2 break;
      }
      rewrite ^/([^/]+)/?$ /index.php?cmd=$1 break;
    }
    IMHO this answers your question and was 2 Minutes of work. It looks to me like you haven't even tried that, which doesn't exactly improve my motivation to help.
     
  12. Emsanator

    Emsanator Member

    I tried it before, and unfortunately, it didn't work. I've always encountered the 404 Error in this code. Not only that, I think I tried these codes that I saw on the internet and that are likely to work.
     
  13. Steini86

    Steini86 Active Member

  14. Emsanator

    Emsanator Member

    This code: "rewrite_log on;" I placed the code in "nginx.conf" located in "/etc/nginx".

    upload_2021-12-27_1-22-10.png

    Site Internal gave a 500 error. Error file content: https://pastebin.com/N8bzSRR6

    My second test;
    upload_2021-12-27_1-23-14.png
    This code worked and the "error_log" file was clean. Removed the ".php" suffix from the end of the files. Rewrite partially worked, but not see Ajax requests. (404) For example; https://panel.example.com/app/controller/ajax/collect
     
  15. Emsanator

    Emsanator Member

    Hi, any help?
     
  16. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    I think I mentioned before this i.e. in your other thread, that nginx directives in ISPConfig do not work the way you put it. :rolleyes:

    You should use something like this:
    Code:
    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    try_files $uri $uri/ /index.html;
    }
    
    What you want to achieve seems to me should be done by overwritng the main master conf file for nginx. If that is true, you will need to copy nginx master conf file from /conf/ to /custom-conf/ folder and modify it in the latter. However, this will make all websites in that server using the same template. Otherwise, if this is meant for single website only, that modify its own individual nginx conf file and use chattr to ensure it won't be changed.

    This will definitely come back to what I said earlier which is this will depends on that website sub domain setup whether on its own, under another main domain without vhost or with its own vhost.

    I can't help much without knowing what software that you are using, your intended structure, design and/or way you intend to achieve. All I can say, it is very much possible to do it with ISPConfig with LEMP as you intended to do it as much as it can be done with LAMP which is the easier way for you to it.

    About the issue of fast, both nginx and apache2 are almost the same but nginx will need a lot more technical knowledge. I converted my ISPConfig to nginx since almost 7 years ago and have been using it until now thus I think know what I am pretty sure what I was talking about. ;)
     
  17. Emsanator

    Emsanator Member

    Last night, I found the solution to the problem. Now I can say that this topic has reached its goal. Thank you for taking the time to help you and other valuable people. This question is over but a new problem arose right after that, but it's not Nginx related, it's purely ISPconfig related problem. I'm dealing with it right now :)

    Thanks again for your interest, best regards.
    (benim saat dilimimle)
     
    Last edited: Jan 3, 2022
  18. till

    till Super Moderator Staff Member ISPConfig Developer

    And what is the solution that you found?
     
  19. Emsanator

    Emsanator Member

    I did not share details because there is no general solution. Because it cannot be called a "real solution".
    Router change in the application and Nginx directives "rewrite ^/([^/]+)/([^/]+)?$ /index.php?cmd=$1&scd=$2 break;" I made some changes in this line application-specific and it worked.
     
  20. till

    till Super Moderator Staff Member ISPConfig Developer

    You claimed in post #17 that it's an issue in ISPConfig (which means you must have altered ISPConfig source code to get it working), which you don't seem to have done. I don't see from your description what the issue in ISPConfig should be as ISPConfig is not even involved in the whole URL rewriting, it's done purely by nginx together with your application and it would have been the exact same thing if you would have run a nginx server with your application without having ISPConfig installed.
     

Share This Page