.htaccess subdomain redirect - .htaccess

My server is somehow configured to accept any string as a existing subdomian. This subdomains doesn't redirect, they show content instead. For example:
bla.example.com // shows the content of http://example.com
blabla.example.com // show the content of http://example.com
lorem.example.com/events/ // shows the content of http://example.com/events
So you can use "stackoverflow.example.com" and it will work.
I want to create an .htaccess rule to overwrite this behavior. I want to redirect http://*.example.com/* to http://www.example.com/*
Everything I have done so far is redirect http://example.com to http://www.example.com (wich is another behavior I want to keep)
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Thank you for your help!

Try adding the following to your htaccess file in the root folder of your domain
RewriteEngine on
RewriteBase /
#if its not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
#redirect it to www.example.com
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]

Related

Redirect non www to www sub pages also

in .htaccess Am using :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/$1/ [R=301,L]
It works fine for main domain i.e. redirects example.com to https://www.example.com
but not works on sub pages like
site.com/blog not redirects to https://www.example.com/blog
also like to add tailing slash '/' after url
https://www.example.com/blog/
how to achieve this?
Thanks in advance!
You should have htaccess rule like this. Make sure you keep these rules at the top of your htaccess file. Please clear your browser cache before testing your URLs.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}/ [NE,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]

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

How to load the same website after redirecting subdomain to subdirectory htaccess

I could redirect subdomain to subdirectory:
sub.domain.com > www.domain.com/sub
By using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.domain.com [NC]
RewriteRule (.*) www.domain.com/sub/$1 [R=301,L]
However, the page at www.domain.com/sub displayed nothing but this:
Index of /sub
Parent Directory
Apache Server at www.domain.com Port 80
I have a working website with contents at sub.domain.com.
How can I load the same website and make it work at www.domain.com/sub ?
Thank you.
Jon is right, you need to move the content. Then you just need to fix your redirect by adding http:// in front of the target domain to redirect incoming requests from the old subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/sub/$1 [R=301,L]

.htaccess removing www. AND redirect

I have been trying to redirect old pages for my old site to the corresponding new ones with permanent redirect.
as well as redirecting www.example.com to example.com
I dont seem to able to do both on the same time
at the moment redirects works for correct links from ex www.example.com/correctlink to example.com/correctlink
but only example.com/Info.aspx is redirected to example.com/about-magento-demo-store and NOT www.example.com/Info.aspx
*Update
I want to remove www. AND redirect 40-50 specific adress to new specific adresses. My problem is that the redirect only works if google has saved the old link without Www. IF google has stored a link including www. then my redirect Redirect permanent example.com/tabid/61/CategoryID/13/ProductID/64/Default.aspx /index.php/solpaneler/re does not function –
*Update
my htaccess looks a bit like this
(with a few lines in the end that where present before I started editing)
(i also tried to ad a line Redirect permanent http://www.example.com/Info.aspx http://example.com/about-magento-demo-store but it does not function)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.se/$1 [R=301,QSA,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
Redirect permanent /Info.aspx /about-magento-demo-store
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule .* index.php [L]
Just use rewrite rules for everything. Rewrite rules and Redirect don't always mix well.
RewriteRule ^Info\.aspx$ /about-magento-demo-store [L,R=301]
I've had the same problem a looooooooong time ago. Here's a sample of my rewriterules:
# If domain name doesn't end with .com redirect to .com:
RewriteCond %{HTTP_HOST} (.*)\.(fr|net|org|eu) [NC]
RewriteRule (.*) http://%1.com$1 [R=301,L]
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# Ca signifie forcément que c'est sans www => forcer redirection :
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
NB: Put this on the top of your rewrite rules, because it should be processed before anything else so that you are sure your domain name always begin with "www"
Note that it redirects mydomainname only if it's "empty" i.e. nothing behind. It won't touch URLs like http://abc.mydomainname.com and it won't touch URLs like http://abc.def.mydomainname.com
Tell me if it works.

Resources