If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Redirecting from subdomain to domain .htaccess

Started by toolforsek, Apr 27, 2023, 06:40 AM

Previous topic - Next topic

toolforsekTopic starter

Greetings!
Can you explain the procedure for using .htaccess to redirect from a subdomain to the root domain? The requirement is to transfer the user from subdomen1.mysite.com to mysite.com/index.php?action=1 while retaining the subdomen1.mysite.com address in the address bar.

Additionally, is there any specific reason why the redirect must occur via .htaccess?
  •  


RickyChhajed

To configure your virtual host, access your Apache config and make the following changes: <virtualhost> ServerName domain.com ServerAlias subdomain.domain.com # ... </virtualhost>.

Some hosting providers will offer site alias options, where you can specify additional domains to point to your main website. For example, when creating domain.com, you might be prompted for aliases such as www.domain.com.
It's worth noting that www is just a subdomain.
  •  

Cydendorzhi

For optimal results, it is recommended to conduct the redirection process separately by creating a distinct "site". If the subdomain previously hosted a different site than the one on the main domain, a template redirection can cause significant damage to the site.

In such cases, it is advisable to use redirection only from the main subdomain and place a comprehensive 404 page with necessary links on the internal ones. Additionally, you can set up individual redirects for specific internal addresses, which our service supports. It's unclear how many people on the forum have implemented this setup.
  •  

snowy94

To start from the beginning, I'm in the midst of learning php OOP through a combination of video courses and articles. Recently, I stumbled upon a course that teaches how to create my own framework by configuring the .htaccess file and front controller. Rather than using GET parameters, the framework relies on routes consisting of sets of parameters with slashes. Although I am still repeating and commenting on some parts of the course where I am fuzzy, I have only ever known PHP at the level of writing functions.

Additionally, I recently created a landing page for a company's subdomain and we decided to transfer it to the main domain. As I looked into redirecting the pages, I realized that the route was empty for some reason. Despite making changes to the .htaccess file and index.php, I couldn't understand why the route wasn't being passed.

While I've since moved on from this issue, I worry that it may resurface if I encounter the same hosting service in the future and need to build a more complex system from scratch.
  •  

alexfernando

Let's address the second part of your question: "Is there any specific reason why the redirect must occur via .htaccess?" The .htaccess file is an Apache web server configuration file that allows you to make various server-level modifications without directly accessing the main server configuration files. It provides a convenient way to implement redirects, rewrite rules, and other customizations on a per-directory basis.

While it is possible to achieve the same functionality through server configuration files or within your application's code, using .htaccess offers several advantages:

1. Portability: The .htaccess file can be easily transferred between different hosting environments, making your website more portable and independent of the server configuration.
2. Ease of modification: You can update the .htaccess file directly without requiring administrative access to the server configuration files, which can be more complicated and potentially risky.
3. Granular control: With .htaccess, you can apply rules and configurations at a directory level, allowing for more fine-grained control over different parts of your website.

Now, let's dive into the procedure for using .htaccess to redirect from a subdomain to the root domain while retaining the subdomain in the address bar:

1. Create or locate the .htaccess file in the root directory of your website. If the file doesn't exist, you can create a new text file named ".htaccess" (without any extension).

2. Open the .htaccess file in a text editor.

3. Add the following lines to the file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomen1\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/index.php?action=1&%{QUERY_STRING} [L,P,QSA]


Let's break down the code:

- `RewriteEngine On`: This line enables the Apache rewrite engine, which is necessary for the redirection to work.
- `RewriteCond %{HTTP_HOST} ^subdomen1\.mysite\.com$ [NC]`: This condition checks if the incoming request is for the subdomain "subdomen1.mysite.com". The `[NC]` flag makes the match case-insensitive.
- `RewriteRule ^(.*)$ http://mysite.com/index.php?action=1&%{QUERY_STRING} [L,P,QSA]`: This rule performs the actual redirection. It matches any URI (`^(.*)$`) and redirects it to `http://mysite.com/index.php?action=1`, appending any existing query string parameters (`%{QUERY_STRING}`) to the new URL.
  - `[L]`: This flag indicates that if the rule matches, no further rewrite rules should be processed.
  - `[P]`: This flag ensures that the redirect is permanent (HTTP 301 status code).
  - `[QSA]`: This flag preserves any existing query string parameters and appends them to the new URL.

4. Save the .htaccess file.

After implementing these steps, when a user visits `subdomen1.mysite.com`, they will be redirected to `mysite.com/index.php?action=1`, with any additional query string parameters appended. However, the address bar will still display `subdomen1.mysite.com`, as per your requirement.

It's important to note that for this redirection to work, the Apache rewrite module (`mod_rewrite`) must be enabled on your server. If you encounter any issues or need further assistance, consult with your hosting provider or server administrator.

By leveraging the power of .htaccess, you can achieve complex URL manipulations and redirections without modifying your application code directly, making it an invaluable tool for web developers and administrators.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...