Redirecting a subdomain with .htaccess

I have a subdomain set up on my hosting: indiantimes.indianradio.net.au, that is being pulled from a folder in my /public_html folder: /public_html/indiantimes.com.au.

I am trying to write an .htaccess rule that will redirect it to that folder still, but retain the original url the user typed in: indiantimes.indianradio.net.au.

I have only been able to get the redirect working, i.e. (indiantimes.indianradio.net.au redirects to indianradio.net.au/indiantimes.com.au/), but I can't seem to get the redirect working so the url seen by the user, stays at: indiantimes.indianradio.net.au. The majority of the image urls are broken intil I am able to get the redirect working properly.

The .htaccess rule I was playing around with was:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.indiantimes\.indianradio\.net\.au$
RewriteRule ^/?$ "http\:\/\/indianradio\.net\.au\/public_html\/indiantimes\.com\.au" [R=301,L]

What am I doing wrong with the redirect? Any help would be much appreciated! Thanks in advance!

Upvotes: 0

Views: 269

Answers (2)

Galeb Nassri
Galeb Nassri

Reputation: 874

You have to replace your sub folder name to be the same as your sub domain
(indiantimes.com.au -> indiantimes).

RewriteEngine On

RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$
RewriteCond %{REQUEST_URI} !^/indiantimes/
RewriteRule (.*) /indiantimes/$1

source

Upvotes: 1

Jonas D.
Jonas D.

Reputation: 371

for subdomains it is generally recommended to add a virtualhost in apache instead of using .htaccess (preformance-wise and more cross-platform).
However you might find the following link suitable in case editing the apache config files isn't an option: .htaccess rewrite subdomain to directory (summary: using mod-proxy and adding the P flag to your RewriteRule)

  1. Go to /etc/apache2/sites-available (use cd in terminal)

  2. Add a new file named: indiantimes.indianradio.net.au, example content:

    <VirtualHost *>
        DocumentRoot /var/www/indianradio.net.au/public_html/indiantimes.com.au/
        ServerName indiantimes.indianradio.net.au
    
        <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews +Includes
            AllowOverride None
            Order allow,deny
            allow from all
        </Directory>
    
    
        ErrorLog ${APACHE_LOG_DIR}/error-logfile.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
    
    </VirtualHost>
    
  3. link to the file in apache2/sites-enabled

in terminal: ln -s ./indiantimes.indianradio.net.au ../sites-enabled/ from the sites-available folder, note the trailing /!

Upvotes: 0

Related Questions