.htaccess redirect without change url same host - apache

I want to redirect a subdomain inside panel, the subdomain editor.domain.com
It should redirect to domain.com/cmseditor.
However the visitor should see editor.domain.com.
So far I've tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^editor\.homecatering\.pt$ [NC]
RewriteRule ^ http://homecatering.pt/cmseditor%{REQUEST_URI} [L,R=301]

Try something like this:
RewriteEngine On
# If the subdomain is "editor.homecatering.pt"
RewriteCond %{HTTP_HOST} ^editor.homecatering.pt$ [NC]
# Then rewrite any request to directory /cmseditor
RewriteRule ^((?!cmseditor).*)$ /cmseditor/$1 [NC,L]
or
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.homecatering\.pt$ [NC]
RewriteRule ^(.*)$ http://homecatering\.pt/cmseditor/%1

Related

htaccess rewrite rules - domain/subdomain https/www

I have my main domain and a subdomain. Subdomain is located in a subdirectory on the root directory of main domain and this cannot be changed. Main domain has an SSL while subdomain doesn't.
Now I am trying to force https and www for the main domain and force no https and no www for the subdomain. So at the end the main domain disregarding how it will try to be accessed should redirect at https://www.example.com and same time subdomain should be http://sub.example.com
All other possible combinations of accessing them should redirect to the above 2. So far, I am successfull to do this for the most possibilities but I am failing to do so for the case when I access the subdomain with https://sub.example.com. It won't redirect to the http version, rather it will try to load the page through https, browser throws the security warning about the SSL and if I proceed, it load the contents of the main domain site through the subdomain and the insecure warning on the browser.
Following is the htaccess rules I am trying, on the htaccess on the root directory, that affects both main site and subdomain.
How to improve them and achieve the desired outcome?
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\.example\.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !sub\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www\.sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
To redirect only the main domain to https, you can use the following rule :
RewriteEngine on
#https to http (subdomain)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [NE,L,R]
#main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

Redirect all subdomain to main domain except one selected

If I want to redirect all subdomains to a main domain, except one subdomain that I need redirect to a subfolder. Is there any way?
Redirect selected subdomain to subfolder
Redirect all subdomains to main domain
This is my Apache configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/mySubdomain [L,R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) http://domain.com/$1 [L,R=301,QSA]
The first rule is correct to serve a particular subdomain from a directory. For the second rule just add 1 AND condition that if the current request is not for domain.com AND not for mySubdomain.domain.com then redirect request to domain.com as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/mySubdomain/$1 [R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

Using htaccess to redirect all subdomains except one

current htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.co.uk [NC]
RewriteRule ^(.*)$ http://www.mywebsite.co.uk/$1 [L,R=301,NC]
I also have a subdomain :
m.mywebsite.co.uk
I don't want this "m." domain redirected, but all other subdomains that exist or not directed to www.mywebsite.co.uk including "mywebsite.co.uk"
Can you help?
thanks
Try :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^m\.mywebsite\.co\.uk$
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.co.uk/$1 [L,R=301,NC]

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

Rewrite URL using Apache for redirecting root domain

I have this scenario where I would like to redirect my domains using the following scenario. Can you please advice if this can be achieved using RewriteRule in Apache?
I would like to redirect any calls made using http://www.domainname.com/url to redirect to http://domainname.com/url.
I was able to achieve the above using the following rewrite rule
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
# END WithoutWWW
If anyone tries to visit just http://www.domainname.com or http://domainname.com, I would like them to redirect to http://dname.com
How can I achieve this rule using RewriteRule in Apache?
I'm also using PHP, so a solution using PHP would be valid too.
Here is your combined .htaccess with your existing and new code:
RewriteEngine on
# redirect domainname.com/ or www.domainname.com/ to dname.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com [R=301,L]
# append www to domainname.com
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^ http://domainname.com%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
That example looks like mod_rewrite.
With mod_rewrite you could do:
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^(.*)$ http://dname.com/$1 [R=301,L]
# END WithoutWWW
You may want to look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Based on your comment you want to redirect the root differently so we could do:
RewriteEngine on
#www.domainname.com & domainname.com -> dname.com
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com/ [R=301,L]
#www.domainname.com/[url] -> domainname.com/[url]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]