.htaccess rewrite for subdomain only - .htaccess

I have the following .htaccess code that successfully rewrites subdomain directories. So if a user visits jim.domain.com they get served placeholder.php?page_type=profile. However, this also affects the main directory http://domain.com. I'd like it so that anything with a subdomain is directed to the placeholder.php page, but using the main directory with no subdomain would act as usual, going to the index.php in the main directory. How would I achieve this? Thanks for any advice.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ placeholder.php?page_type=profile [L,QSA]

If your wanting all requests for test.domain.com to go to domain.com/placeholder.php?page_type=profile as in your comment then you need to include the domain in the rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://domain.com/placeholder.php?page_type=profile [L,QSA]
this will not how ever deal with this part
but using the main directory with no subdomain would act as usual, going to the index.php in the main directory
because you have no file/directory miss rules that redirect to index.php. Do you need those, or really just want to solve the first part?
I also wonder if your wanting profile replaced with the subdomain match, in which case you want
RewriteRule ^$ http://domain.com/placeholder.php?page_type=%1[L,QSA]
but both of these examples will not rewrite any path of test.domain.com/longer_url because the ^$ is only matching an empty URL path. But how you want to handling paths needs to be decided before a suitable Reerite can be proposed.

Related

.htaccess redirect subdomain to php file without URL change in browser

help me please.
I have several php files in root
domain.com/dog.php
domain.com/cat.php
...
When user go to dog.domain.com i need to show domain.com/dog.php but keep dog.domain.com in browser URL
I already have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteRule .* http://domain.com/%1.php [L]
It works fine and show domain.com/dog.php content when I type dog.domain.com, but now it changes URL in browser to domain.com/dog.php
How to solve it?
P.S. Wildcard DNS record turned ON (already catch all subdomains *.domain.com)
If *.domain.com is defined within the same vhost you have to use a relative RewriteRule there: RewriteRule ^.*$ /%1.php [L]). If not, you have to replace [L] by [P] and make sure that you have mod_proxy loaded. See https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Last but not least, you might want to update your RewriteCond to be case insensitive: RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]

Redirect subdomain of one domain to a subdomain of another domain

I am trying to redirect the following (respectively):
http://sub.firstdomain.com/d/(all_files_and_folders)
http://sub.firstdomain.com/d2/(all_files_and_folders)
to
http://sub.seconddomain.com/d/(all_files_and_folders)
http://sub.seconddomain.com/d2/(all_files_and_folders)
There are other files and folders under the first subdomain that I do not want redirected. Previously this was working but now it seems like Go Daddy changed something and what I had is no longer working. Here it is:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.+) /dstats/count.php?statspage=$1 [L,NC,QSA]
RewriteRule ^(.+)\.deb$ /dstats/count.php?file=$1.deb [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(d2?)/(.*)$ http://sub.seconddomain.com/$1/$2 [L,R=301]
You can ignore the RewriteRule. It is working fine. I wanted to make sure I included the entire .htaccess file just in case. My issue is with RewriteCond it seems.
The following should work:
RewriteEngine On
Redirect 301 /d/ http://sub.seconddomain.com/d/
Redirect 301 /d2/ http://sub.seconddomain.com/d2/
The above should only redirect anything in the /d/ and /d2/ folder of the sub subdomain.
EDIT: Second try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]

RewriteRule multilanguage site

Have site with many languages
extra languages are in subdomains on my server like de.mydomain.com, fr.mydomain.com.
in these de,fr subdirectory i've placed only htaccess file with this code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.de\.mydomain\.com$
RewriteRule ^(.*)$ "http://www.mydomain.com/$1?lan=2%1" [L]
And it works but not how I want. it simply redirects from de.mydomain.com to mydomain.com, url in the browser is changing and I would like the url to stay like: de.mydomain.com but the content to be taken from mydomain.com?lan=2.(i use lan variable to change lang)
what do I do wrong here?
maybe my general aproach to this problem is wrong?
Edit :
You need to point de subdomain to the root directory instead, and then adding these lines to the root htaccess file :
RewriteCond %{HTTP_HOST} ^(www\.)?de\.mydomain\.com$
RewriteRule ^(.*)$ /$1?lan=2 [L,QSA]
Put this rule before your rule:
RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]
.htaccess rewrite to default language folder?

.htaccess point subdomain to file

What I am trying to do is be able to point something like username.domain.com to index.php?url=username. I have been browsing around Google/StackOveflow and have not been able to find anything that will benefit me.
Best Regards.
Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^ /index.php?url=%1 [L]
The first condition makes sure the requested host isn't www.domain.com or domain.com, the next line matches and groups the subdomain name, and the rule itself rewrites everything to /index.php with a query string url and the subdomain (backreferenced using %1).

Mod_Rewrite with Relative Path Redirects

I have this rule in an .htaccess file located in a directory named clips/:
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
What I am intending is that when someone visits http://example.local/clips/mlk they are redirected to http://example.local/clips/segment/index.php?clip=1
What actually happens is that when someone visits example.local/clips/mlk they are redirected to example.local/var/www/example/clips/segment/index.php?clip=1
I'm not sure why it's doing this. If I change the rewrite rule to this:
RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L]
The user is redirected to example.local/segment/index.php?clip=1, which is still incorrect. I don't want to have to specify an absolute path in the case of these files being moved around the website's directory tree. How can I get this to work relatively instead of absolutely?
Try adding a RewriteBase directive as below
RewriteEngine On
RewriteBase /clips/
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
EDIT
but is there any way to get this to work without using a RewriteBase directive
You could also try
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC]
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]

Resources