简体   繁体   中英

.htaccess HTTPS redirect without changing url

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).

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

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(?:\Comodo\DCV)?$
RewriteRule ^(.*)$ /mobile.php/nb/$1 [L]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM