Wildcard subdomain doesn't include subfolders - .htaccess

In cPanel (at subdomain management) I've set up a wildcard like to following:
*.example.com > public_html/subDomainHandler
Every (non existent subdomain) request like abc.example.com redirects properly to example.com/subDomainHandler.
But requests like abc.example.com/subfolder doesn't redirect to example.com/subDomainHandler.
I am not at my best in regular expressions, but can anybody give me a RewriteCondition for .htaccess?

Htaccess Rewrite Rule - Try1
I think your answer could be modeled on https://stackoverflow.com/a/8481076/1688441 .
Try the following:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com\(.*)
RewriteRule ^(.*)$ http://example.com/subDomainHandler/$1 [R=301]
Try2
Another option:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/subDomainHandler%1 [R=301,L]
From: https://webmasters.stackexchange.com/questions/13475/redirect-rewrite-subdomain-to-subfolder

Related

How to hide .htaccess redirect from user

I have server with domain www.domain.com and multiple sub domains sub1.domain.com, sub2.domain.com. They are all pointing to server root.
I'd like users to access specific folders by subdomains. For example:
sub1.domain.com/someURI => sub1.domain.com/subFolder1/someURI
sub2.domain.com/someURI => sub1.domain.com/subFolder2/someURI
I would like to hide these redirections from users. I tried following .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.domain.com$
RewriteRule ^(.*)$ http://sub1.domain.com/subFolder1/$1 [R,L]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$
RewriteRule ^(.*)$ http://sub2.domain.com/subFolder2/$1 [R,L]
It is redirecting correctly only without any URI and redirection is visible.
You have 2 issues that are causing external redirect:
Using R flag in RewriteRule
Using Absolute URL starting with http:// in target
Another issue is that your rewrite rule is unconditional which can cause infinite looping.
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =sub1.domain.com
RewriteRule ^((?!subFolder1/).*)$ subFolder1/$1 [NC,L]
RewriteCond %{HTTP_HOST} =sub2.domain.com
RewriteRule ^((?!subFolder2/).*)$ subFolder2/$1 [NC,L]

Very simple Apache htaccess redirect

What I want to do:
Redirect this:
http://example.com/9hyf4dx
... and this:
http://www.example.com/9hyf4dx
to:
http://example.com/special.php?var=9hyf4dx
Try this (it uses a regular expression if the variable you posted is just an example):
RewriteEngine on
RewriteRule ^([a-z0-9]+)*$ special.php?var=$1
if you want you can also add a rule that all visits to your site will have to have the WWW:
#force WWW on main domain
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Add parameter for specific domains in .htaccess rule

I have two domains that point to the same webserver:
example.cz
example.de
I need:
example.de → example.de/de (but can't be see in address field: example.cz/de)
If it ends up showing in the URL like example.de/de it's OK, but the best solution is just example.de and from server load example.de/de.
I've tried with this:
RewriteBase /
RewriteCond %{HTTP_HOST} example.de$ [NC]
RewriteRule ^$ example.de/de [L,R=301]
but after I click on something on the page I get: example.cz/de and this is problem.
You can use an inner rewrite without R=301 redirect, if you use LAMP server, in /.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?example\.de$ [NC]
RewriteRule ^$ /de/index.php [L]
You'll see in address bar: http://example.de but not http://example.de/de .
Add this to redirect http://example.cz/de to http://example.de .
RewriteCond %{HTTP_HOST} (www\.)?example\.cz$ [NC]
RewriteRule ^de$ http://example.de [R=301,L]

Redirect using .htaccess so that users appear as a subdomain

I have been using .htaccess for few redirects, I am having a hard time finding any solution for a new redirect I want.
I have users on my site and urls look like:
http://www.domain.com/users.php?user=username
Is it possible to rewrite it using htaccess so that it appears:
http://username.domain.com
Any help is much appreciated.
Something along these lines:
RewriteRule ^users.php?user=(.*)$ http://$1.domain.com/ [R,L]
As long as you've set it up so that all subdomains of domain.com point to the same document root as domain.com then you can do this with a single htaccess file in your document root:
RewriteEngine On
# externally redirect to user's subdomain when a specific request is made to users.php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /users\.php\?user=([^&\ ]+)
RewriteRule ^ http://%2.domain.com/ [L,R=301]
# internally rewrite the user's subdomain to users.php
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^/?$ /users\.php?user=%1 [L,QSA]

Optimize htaccess Wildcard Subdomain Code

I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

Resources