.htaccess redirect to subdirectory if no 'www.' in requested URL - apache

I'd like to keep users at the main directory of my domain if they enter the domainname as www.mydomain.de , but redirect them to a subdirectory if the domain is entered without the "www.". I know it makes little sense, but I desparately need it for temporary testing purposes while preparing a website.
I've tried with
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^mydomain\.de$ [NC]
RewriteRule ^/(.*)$ http://mydomain.de/subdir/$1 [L]
but it doesn't do the trick. Can anyone point me to the right direction please?

Try removing the forward slash in the rewriterule and change your condition. You can try it this way.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^/?$
RewriteCond %{HTTP_HOST} ^mydomain\.de$ [NC]
RewriteRule ^((?!subdir).*)$ http://mydomain.de/subdir/$1 [L]

Related

Apache wildcard subdomains with url masking

I am trying to handle wildcard subdomains with url masking in apache.
Correct rewrite rules should achieve the following:
http://demo.system.dev to to http://system.dev?subdomain=demo
http://sample.system.dev/user/edit/100 to to http://system.dev/user/edit/100?subdomain=sample
http://debug.system.dev/project/edit/new to to http://system.dev/project/edit/new?subdomain=debug
So far i have the following rule in my .htaccess
RewriteCond %{HTTP_HOST} ^(.*)\.system\.dev
RewriteRule ^(.*?)$ http://system.dev%{REQUEST_URI}?subdomain=%1 [L]
which looks like its working ok except that the browser url is also changed. I would like the browser url to remain the same and internally route the request but i am not sure how to achieve this.
Try this rule in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) $0?subdomain=%1 [L,QSA]
You can not internally rewrite to another domain. So when you go to your subdomain it will redirect to main domain as you have it. So you will need to use relative URL and see if that works for you.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?system\.dev [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?subdomain=%1 [L]

Redirecting subdomain to file without hardcoding url

Actually topic is simple and answered million times but, somehow doesn't work on me.
What I try to do is redirect "m.domain.com" and "mob.domain.com" to a file "domain.com/folder/mobile.htm"
But looks like any code conflicts with my current code which removes "www" from beginning.
My current code is
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
# this is for webmail forward #
RewriteCond %{REQUEST_URI} /mail/?$ [NC]
RewriteRule . /webmail/ [L]
How can I redirect m and mob domains without conflicting with this code and without hardcoding "domain.com" ?
Thanks for your helps in advance.
Put this rule on top of your existing rules just below RewriteEngine On line:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:m|mob)\.(.+)$ [NC]
RewriteRule ^$ http%1://%2/folder/mobile.htm [L,R=301]

Folder to subdomain when autoappending www

We have a .htaccess-file like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
RewriteRule (.*) http://www.mysite.no/$1 [R=301,L]
Which autoappend www in front of the URL if it's missing. In addition we have to expand our site to a mobile version that we'd like to call m.mysite.no.
How do you do this and make the mod_rewrite ignore the www-rule? Unfortunately, this site is live, so we can't do much testing and we have little experience on the field.
Change:
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
To:
RewriteCond %{HTTP_HOST} !^(www|m)\.mysite\.no [NC]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite and subdomains

I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:
php_flag display_errors off
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:
RewriteRule ^(.+) /online_content/$1 [L]
I get an error
I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.
And yes, I do need to rewrite to a directory that has a different name than the subdomain.
Well, I figured it out.
The issue was that I was causing an infinite loop.
Once the rewrite had happened, it was still trying to rewrite to the directory.
Here is my new htaccess that took care of it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]
Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.
Try this rule:
RewriteCond %{HTTP_HOST} =onlinedev.example.com [NC]
RewriteCond $0 !^online_content/
RewriteRule .+ /online_content/$0 [L]