Redirecting all subdomains to one page - .htaccess

I'm trying to redirect all subdomains at (http://*.domain.com) to spesific html file (http://domain.com/index.html). But I also want to visible subdomain name won't change.
I've tried this one with .htaccess:
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_URI} !^/parent/
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule (.*) /index.html [L]
But it doesn't work at all.
Can anyone help me?

Subdomains are a bit different than directories. You need to have a wildcard DNS record to catch all subdomains:
*.example.com. 3600 IN A host1.example.com.
Best thing to do would be to contact your hosting provider. Some of them have settings to achieve that.

Related

How to redirect all users to maintainence page except certain IPS through .htaccess

I am new to .htaccess and not able to write conditions in .htaccess as per my need. I have tried many similar solutions shared by other users but still problem is not resolved. So that's why I am putting my issue separately here.
I have a website but for few days, it is going in maintainence mode. For this, I have created maintainence.php file in the root directory. So, if any user hit any url under this domain then they will get message written in maintainence.php file.
But I want to allow some IPs to access whole website, any url under the domain. I am trying to do this thing using .htaccess. And I think doing this with .htaccess file is the best way.
What I have tried is given below:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^xxx\.xx\.xx\.xx$ #this is IP that I want to allow
RewriteCond %{REQUEST_URI} !=/maintainence.php
RewriteRule ^(/.*)?$ /maintainence.php [R=301,L]
</IfModule>
The above solution is redirecting everyone to maintainence.php including the mentioned IP.
Can anyone please help me on this? Thanks in Advance
You are almost deny that IP not allow it , so if you want to allow some IPs and prevent others just fix your code like this :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^xxx\.xx\.xx\.xx [OR]
RewriteCond %{REMOTE_ADDR} !^xxx\.xx\.xx\.xx [OR]
RewriteCond %{REMOTE_ADDR} !^xxx\.xx\.xx\.xx
# i just excluded three but you could do less or more
RewriteCond %{REQUEST_URI} !=/maintainence.php
RewriteRule ^(.*)$ /maintainence.php [R=301,L]
</IfModule>
Note: clear browser cache the test it.

.htaccess redirect everything from directory to a different domain

We have a WordPress site/single app which serves two domain names: example.com and example.es.
In our .htaccess file we have currently have the following rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.es
RewriteRule ^(.*)$ http://www.exmaple.es/$1 [R=permanent,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
We also need to redirect everything from example.com/es/... to example.es/.... I have tried multiple examples that I could find, but none of them worked (ended up in 500 server error and empty log), here are a few of them:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) http://www.example.es/%1 [R=302,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/es [NC]
RewriteRule ^(.*)$ http://www.example.es/$1 [R=301,NC,L]
All of the new rules were placed inside the <IfModule mod_rewrite.c>
Any help or guidance is much appreciated.
#Domas Hi Domas, the process in your case is to update you site installing some important extension about the domain change.
Changing the preferred domain name alias WordPress uses
If your WordPress site has a domain name alias pointing to it, and you want WordPress to prefer the alias domain name in links that it generates, you can update the domain name in the WordPress dashboard:
Click Settings.
Change the WordPress Address (URL) to the new URL that you want to use.
Change the Site Address (URL) to the same value.
Click Save Changes.
You need WP Super Cache, this extension help you to update automatically the .htaccess and change all the needs as you did on you configuration site.
First install the extension https://support.tigertech.net/move-wordpress#updating-htaccess-wp-jmp
Also if you don't need any extension and you want to do by the hand just redirect like this example as needs :
#the simple redirection
Redirect 301 /es http://www.example.es/
#Redirect from old domain with subdirectory to new domain
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) http://www.example.es%{REQUEST_URI} [R=302,NC]
It depend of you needs i prefer the last one. Regards

.htaccess redirect/rewrite from one url to another, and subpages to other pages

I'm having some difficulty trying to get a correct rewrite/redirect that will do what I want and I'm wondering if it's even possible.
I have this rewrite setup:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
This works great to take all requests from olddomain.com/whatever and rewrite them to my new main url. However, I also would like to be able to take 10 or 15 of the most popular "old" urls and rewrite/redirect them to the new location on the new url. My directory structure changed with the changeover to new url, so all the new urls are different and things are located in different places. Is it possible to have all traffic from the old domain redirected to the new domain AND specify a few old urls to redirect to new urls on the new domain?
I hope this is clear enough and I'm hoping someone can assist me. Thanks for your time!
Tom
Here's a solution:
.htaccess for old domain
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
In the above, you could place redirect rules to catch certain pages and forward them to their new location, like this:
Redirect 301 /2012/05/old-post http://www.newdomain.com/old-post
Be sure to place the redirect directives above the rewrite rule that catches everything else.
.htaccess for new domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]
Found this solution on: https://forums.digitalpoint.com/threads/htaccess-redirect-all-traffic-to-another-site.867280/
I've done similar things, so hopefully it helps.

Using htaccess to strip domain name from URL

I'm not even sure if this is possible but have been looking everywhere for a solution. My site generates dynamic info about domain names and people can view their info in the following format:
http://www.mysite.com/analyze/yourdomain.com
The problem is that sometimes people will enter http:// or www or even the full URL instead of their domain, such as this:
http://www.mysite.com/analyze/http://www.yourdomain.com/this-is-a-test
What I'd like to do with htaccess is get the domain name and redirect it to the proper URL like the first one listed.
This is what I currently have for my htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteCond %{REQUEST_URI} !\.(xml|txt)$
RewriteRule ^(.*)$ http://www.mysite.com/review/$1 [R=301,L]
Options -MultiViews +FollowSymlinks -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(xml|txt)$
RewriteRule ^([a-zA-Z0-9\.\-]+)$ review.php?site=$1 [L,QSA]
The main issue that I'm having is that every tutorial I find about stripping/redirecting domains is applying to my actual domain name, not the user's in the URL. Any help would be greatly appreciated!
Try adding thie right below your first rule (that 301 redirects):
RewriteRule ^analyze/http:/+([^/]+)/? /analyze/$1 [L,R=301]
This will redirect requests like http://www.mysite.com/analyze/http://www.yourdomain.com/this-is-a-test to http://www.mysite.com/analyze/yourdomain.com.

htaccess redirection from domain.com to sub.domain.com while maintaining file access to folders

I'd like to redirect a website from http:// domain.com & http:// www.domain.com to http://v2.domain.com, while maintaining access to http:// (www.)domain.com/images/*
I've tried several methods but somehow I can no longer access the /images folder anymore.
P.S: both subdomain & root has different content/CMS. Any ideas as to how to implement this?
Thanks
You haven't explained exactly how these are configured. I'm going to assume that www.domain.com and v2.domain.com are different virtual hosts:
You should be able to do this in your .htaccess file or apache configuration for www.domain.com:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !images/
RewriteRule ^/(.*) http://v2.domain.com/$1 [L,R]
I realised something like this could work as well. Probably need some testing, but if this is wrong do comment.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{HTTP_HOST} !v2\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://v2.domain.com/ [R=302,L]

Resources