# Actix Rust: Collision in routes due to subdomain

**URL:** https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953
**Category:** help
**Created:** [January 7, 2022, 10:07am UTC](https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953 "2022-01-07T10:07:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ONiel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/oniel/32/14492_2.png) [@ONiel](https://users.rust-lang.org/u/ONiel)
#### Post date: [January 7, 2022, 10:07am UTC](https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953/1 "2022-01-07T10:07:04Z")

</div>

This question is related to [this question](https://users.rust-lang.org/t/deploying-a-rust-actix-project-on-same-domain-and-vps-as-a-laravel-project/66803). There I asked if it was possible to deploy two applications on the same domain. It is possible using Nginx reverse proxy, but that is too complicated, unstable and results in a barely functioning web-application.

So I decided to include my website, which was previously a separate Laravel project, also in the Rust part of the application.

My situation:  
I have a basic website promoting my application. With URLs like:

```rust
example.com/
example.com/home
example.com/about
example.com/contact
...

```

My web-application provides an unlimited amount of businesses a personalized page. Each business has his own subdomain. So:

```rust
businnesa.example.com
businnesb.example.com
businnesc.example.com
...

```

Each business also has their own pages:

```rust
businnesa.example.com/
businnesa.example.com/buy
businnesa.example.com/contact
...

```

The problem I have now: The routes collide!

Business A has his own about page on `businessa.example.com/about`. But my main site also has a about-page on `example.com/about`!

Route code:

```rust
App::new()
    .service(web::resource("/about").route(web::get().to(business_about)))
    .service(web::resource("/about").route(web::get().to(main_site_about)))

```

How would I solve this? I'd need something like:

```rust
App::new()
    .service(web::resource("/about").guard(IS_SUBDOMAIN).route(web::get().to(business_about)))
    .service(web::resource("/about").route(web::get().to(main_site_about)))

```

I could also make a function `about_page_check_if_subdomain()` which would get executed on both `/about`-paths. And checks if the `HttpRequest` contains a subdomain. If so, go to `about_page_business_page()` else go to `about_page_main_site()`. But that doesn't sound really solid.

How do I detect if a request is for a subdomain, and if so, give another route then without subdomain?

Thanks!

---

<div class="post-metadata">

### Author: ![jer](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jer/32/813_2.png) [@jer](https://users.rust-lang.org/u/jer)
#### Post date: [January 7, 2022, 10:37am UTC](https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953/2 "2022-01-07T10:37:23Z")

</div>

[Using `guard::Host`](https://actix.rs/actix-web/actix_web/guard/fn.Host.html)

---

<div class="post-metadata">

### Author: ![ONiel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/oniel/32/14492_2.png) [@ONiel](https://users.rust-lang.org/u/ONiel)
#### Post date: [January 7, 2022, 10:49am UTC](https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953/3 "2022-01-07T10:49:03Z")

</div>

I solved it the following way:

```rust

.service(
                web::resource("/about")
                    .guard(actix_web::guard::fn_guard(|header| {
                        url_contains_subdomain(&format!(
                            "{:?}",
                            header.headers.get("host").unwrap()
                        ))
                    }))
                    .route(web::get().to(business_about)),
            )
.service(web::resource("/about").route(web::get().to(main_site_about)))

// Function to check if given url contain a subdomain
pub fn url_contains_subdomain(url: &str) -> bool {
    println!("rofl {}", url);

    let url = url.replace("www.", "");
    let mut split: std::vec::Vec<&str> = url.split(".").collect();

    split.len() > 2
}

```

Let me know if someone has a more elegant solution.

---

<div class="post-metadata">

### Author: ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)
#### Post date: [April 7, 2022, 10:49am UTC](https://users.rust-lang.org/t/actix-rust-collision-in-routes-due-to-subdomain/69953/4 "2022-04-07T10:49:24Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
