Folder to subdomain when autoappending www - apache

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]

Related

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

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]

mod_write htaccess with subdomains

I've been trying to figure out this htaccess issue and I can't seem to get it working and getting stuck on 500 errors.
I've looked around at multiple S.O. questions and general google but haven't found any that apply to my particular situation. Here it is:
I have example.com. I want if the user puts in: example.com or www.example.com to go to index.php.
But if the user puts in a subdomain subdomain.example.com; I want it to go to
dashboard.php?val=subdomain
Further more I'd like
subdomain.example.com/contact
to go to
dashboard.php?val=subdomain&page=contact
Here is what I have so far:
RewriteEngine On
RewriteBase /
# If doesn't start with www and it has a subdomain, redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule .? - [E=VAL:%1]
RewriteRule ^/?$ /dashboard.php?val=%{ENV:VAL}&page=idx
RewriteRule ^contact/?$ /dashboard.php?val=%{ENV:VAL}&page=contact [L]
#Note: I have tried this with and without the Env variables and only having 1 RewriteRule
# The "catch all other" redirect for images, files and if they do /cont by accident
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !dashboard\.php [NC]
RewriteRule . /dashboard.php?val=%1&page=idx - [L]
There may be subdomainA and subdomainB, etc so want to do a catch all on subdomains. Hopefully the question makes sense. My Apache knowledge is more limited and most of this is from googling around.
Give the following rules a try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^/?$ /dashboard.php?val=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/dashboard\.php [NC]
RewriteRule ^.*$ /dashboard.php?val=%1&page=$0 [NC,L]

.htaccess redirect on Apache from non-www to www version

I'm trying to redirect my website about dentists in London from the non-www to the www version and I'm using the following code on the .htaccess of my server, but it doesn't work. What do I miss?
Thank you
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.topdentists.co.uk [nc]
RewriteRule ^(.*)$ http://www.topdentists.co.uk/$1 [r=301,nc]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This is more general and will work for any site, not just the one you are currently working on. Taken from: Dense13.com (explanation of the code is in the second comment, underneath the article)
The reason why I think your code didn't work is because the RewriteCond uses a regular expression, but you didn't escape the dot (which is a regex-character).
To properly handle any potential https requests, as well as any strange characters, something like this is usually recommended:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess redirect request from specific domain to another domain

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?
This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]

Redirect subdomain to main domain

I'm dealing with a site that supports both English and Spanish, and I'd like to know how to redirect all the requests to the language-specific subdomains to the main domain (en.mysite.com and es.mysite.com to mysite.com).
The whole site is programmed in PHP and it has a main script index.php that processes the language and section GET parameters and displays stuff accordingly.
Now, I tried to do the following in the .htaccess file in the root of mysite.com. I think it clarifies what I'm trying to do:
RewriteEngine On
# English redirects
RewriteRule ^en.mysite.com$ index.php?language=English&section=Main
RewriteRule ^en.mysite.com/store$ index.php?language=English&section=Store
RewriteRule ^en.mysite.com/create_account$ index.php?language=English&section=CreateAcc
# Spanish redirects
RewriteRule ^es.mysite.com$ index.php?language=Spanish&section=Feed
RewriteRule ^es.mysite.com/comprar$ index.php?language=Spanish&section=Store
RewriteRule ^es.mysite.com/crear_cuenta$ index.php?language=Spanish&section=CreateAcc
But this doesn't work. Can anyone tell me what I'm doing wrong here?
You can't include the hostname as part of the match in a RewriteRule, as it only matches against the URI. You'll need to use a separate RewriteCond that matches against the hostname for each of your rules:
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Main
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^store$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^create_account$ /index.php?language=English&section=CreateAcc
Then the spanish rewrites:
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Feed
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^comprar$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^crear_cuenta$ /index.php?language=English&section=CreateAcc