Rewrite subdomain to subdomain/folder in vhost situation - .htaccess

I'm trying to rewrite abc.example.com/path to abc.example.com/index.php/abc/path using the following .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(abc)\.example\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/abc/$1 [L]
The situation is:
domain www.example.com
vhost www
subdomain abc.example.com
vhost abc
I don't want index.php/abc/ to be shown in the URL.
Does anybody have some suggestions?

With the following code everything you type after the domain will be appended to /index.php/abc
abc.domain.com/URL -> abc.domain.com/index.php/abc/URL
abc.domain.com/URL/ -> abc.domain.com/index.php/abc/URL/
abc.domain.com/URL/URL -> abc.domain.com/index.php/abc/URL/URL
abc.domain.com/URL/URL/ -> abc.domain.com/index.php/abc/URL/URL/
Rewrite code:
RewriteCond %{HTTP_HOST} ^(abc).domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/%1$1 [NC,L]

Try this rule in your .htaccess file:
RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteRule ^index\.php$ index.php/%1%{REQUEST_URI} [L]

Related

.htaccess rewrite domain and subdomain to directory and subdirectory

I'm trying to use my .htaccess file to point domains/subdomains to directories/subdirectories without changing the url in the browser.
Examples of the incoming url and the directory it should point to:
domain1.com/* -> /domain1.com/www/*
foo.domain1.com/* -> /domain1.com/foo/*
bar.domain1.com/* -> /domain1.com/bar/*
domain2.com/* -> /domain2.com/www/*
foo.domain2.com/* -> /domain2.com/foo/*
bar.domain2.com/* -> /domain2.com/bar/*
Here's my current attempt that is continually appending the directory/subdirectory to the url:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:([^.]+)\.)?([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_URI} !^/%2\.%3/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%2.%3/%1/$1 [L,NE,P,QSA,R]
Why so complicated? The following should satisfy all requirements you listed:
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+$
RewriteRule ^ /www%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.[^.]+$
RewriteRule ^ /%1%{REQUEST_URI} [L]
Have a try yourself at made with love
It took a little work, but that to the help of the first response, I was able to get everything working eventually.
Here's my final solution to this issue:
## Turn the rewrite engine on to allow for url mapping to work
RewriteEngine on
## Don't require a trailing "/" for directories
DirectorySlash Off
## If subdomain was missing, redirect into the "www" subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/www/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/www/$1 [END,NC,QSA]
# If subdomain was given, redirect into the given subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/%1/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/%1/$1 [END,NC,QSA]

SSL issue - too many redirects

The website in question is: sophostechnologies.com
https://sophostechnologies.com or sophostechnologies.com works fine and loads in SSL, HOWEVER, if I type www. or https://www. in front of the url name, I get too many redirects loop error.
My .htaccess file looks like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sophostechnologies.com/$1 [R,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^(tmp/)?sitemap.xml$ tmp/sitemap.xml [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.php([0-9]*)|\.json|\.xml|\.tpl|\.phtml|\.ini|\.inc|/)$ index.php?_p=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_p=$1 [QSA,L]
Any help is appreciated.

htaccess forward slash before domain error /www

My site URL structure is showing in search results as "/www.domain.com" (with a forward slash before www)
I believe the problem is with the htaccess file. Does anyone know a fix?
htaccess code is below. Many thanks.
RewriteEngine On
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.*)\.html$ %2$1.php [QSA,L,R=301]
RewriteCond %{HTTP_HOST} ^kentuckycartitleloans.com
RewriteRule (.*) http://www.kentuckycartitleloans.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*about.php$ %2contact.php [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*online-application.php$ %2apply.php [QSA,L,R=301]

I need to force ssl on main domain but not subdomain

The main site at www.mydomain.com is set up as a secure site using an SSL certificate.
I need to create a subdomain that is not secured (http://open.mydomain.com).
I'm hosting with InMotionHosting.com. I've created the subdomain in cPanel and pointed it to the folder at public_html/open (the main site is at public_html/mainsite) but when I try to visit the subdomain URL I get an SSL error in Chrome, and in IE I get redirected to https://www.mydomain.com/open.
How do I fix this?
Here is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Change your rules to this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This worked for me:
<IfModule mod_rewrite.c>
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^sub.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
If you want several subdomains change the line like this:
RewriteCond %{HTTP_HOST} !^(sub|lolo).example.com$

Many RewriteBase in one .htaccess file?

I have a domain and a wordpress-blog on same server. Now I have a problem (surprise). The wordpress is located on /httpdocs/blog/ and domain is pointing to /httpdocs/ and I'm trying to redirect it to /httpdocs/domain/. But, obvisiously, I have permalinks in Wordpress.
Here's my current .htaccess:
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
But as you already propably assumed, this doesn't work. Wordpress' permalinks affects to /domain/ also, so my images and other urls go wrong.
Any advice? Is it possible to use RewriteBase like this?
No, you can only have one base URL. Just rewrite your rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]
RewriteCond %{HTTP_HOST} =example.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
I come to this post when I am trying to find solution for a similar problem. It seems that there can be more then one base URL, but the logic does not stop after rewrite. If the URL hit both rewrite base, all the rewrite will be run. Therefore, the strictest rewrite base should be put at the end of the file. For this example, it should be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Noticed that as both rewrite are being run, so if the rewrite contradicts, you will need to fall back to the accepted answer.

Resources