skip to Main Content

I have a main domain mydomain.com and a subdomain m.mydomain.com, both are SSL secured.

When I access the subdomain m.mydomain.com I want to be redirected on mydomain.com/mobile.php/nb without changing the url…so the url must remain m.mydomain.com

Here is what I came up:

RewriteEngine on  
RewriteCond %{HTTP_HOST} ^m.mydomain.com$ [OR]  
RewriteCond %{HTTP_HOST} ^www.m.mydomain.com$  
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$  
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$  
RewriteRule ^(.*)$ "https://mydomain.com/mobile.php/nb$1" [R=301,L]  

It redirects me right but it doesn’t keep the subdomain url address.

All the solutions I’ve found works only if the sites are not secured(http).

2

Answers


  1. It can, but shouldn’t be done via the .htaccess file.
    This is normally done wihtin your vhost config file

    <VirtualHost *:80>
        ServerName www.example.com
        Redirect / https://secure.example.com/
    </VirtualHost>
    
    <VirtualHost _default_:443>
        ServerName secure.example.com
        DocumentRoot /usr/local/apache2/htdocs
        SSLEngine On
        # etc...
    </VirtualHost>
    

    Source: https://wiki.apache.org/httpd/RedirectSSL

    Login or Signup to reply.
  2. The mod-rewrite R flag performs an external redirection of urls from old location to the new one. If you do not want to redirect your subdomain remove the R=301 flag from your RewriteRule and use an absolute path instead of the full url as Rewrite destination.

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(www.)?m.mydomain.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[ 0- 9a -zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0- 9]{ 32 }.txt(?:ComodoDCV)?$
    RewriteRule ^(.*)$ /mobile.php/nb/$1 [L]
    

    This will internally redirect www.m.domain.com or m.domain.com to /mobile.php/nb/ .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search