Permanent redirecting to different domain via .htacces - .htaccess

I got two domains. Lets say dom.com and dom.net.
Now I got alias on dom.net to dom.com, so when I come to dom.net it shows me dom.com - this is set on admin's webpage.
But I need when somebody comes from dom.net redirect him to dom.com/test/example.html.
My .htacces file is blank now.
Is it possible to do this using .htacces and how can it be done?

I got it!
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} dom.net$
RewriteRule ^(.*)$ http://www.dom.com/test$1 [R=301,L]
this code redirect me to test folder but still don't know how to redirect to .html page

Related

How to add www before subdomain in htaccess

So, we printed some cards but the card accidentally has www.sub.domain.com/example but when to try to go there it says the site cannot be reached but If we try to go to sub.domain.com/example it works.
The main website is made in WordPress but the landing pages (subdomains) are made using Unbounce can this be fixed using .htaccess? if so then which .htaccess file and how can we fix this.
Thank you
You need to redirect your www to non-www. That can be done by adding this to your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Don't forget to change yourdomain with your domain.

HTACCESS Redirects and How to Properly Redirect to a domain to the Home Page

I have a domain: soloENEGYBAR.ca which is properly redirecting to soloNUTRITION.ca as long as it's http://
This does NOT WORK for https:// and errors (I do not know why).
THE GOAL
Is there a way to make it so the sub folders for soloenergybar.com ALSO redirect to the home page for solonutrition.ca? As well as get this to work for http:// and https:// instead of just http://
For example, right now if I go to: soloenergybar.ca/en/home it redirects to solonutrition.ca/en/home which doesn't exist!
I would like to know if there's a way to force EVERY URL combo from soloenergybar.ca to go to the home page of solonutrition.ca vs adding the additional folder tree info /en/home/?
Thank you!
Current HTACCESS setting:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^132\.148\.131\.136
RewriteRule (.*) "https://www.solonutrition\.ca\/$1" [R=301,L]
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} soloenergybar\.ca$ [NC]
RewriteRule ^ https://www.solonutrition.ca/? [R=301,L]
Make sure to clear your browser cache or test in a new browser to avoid old cache,

.htaccess make subdomain the main domain without moving

I have my website in a subdomain:
subdomain.mydomain.co.uk
Now, I don't want to move subdomain.mydomain.co.uk but I want to use .htaccess to set it so that when someone types: mydomain.co.uk the url still remains mydomain.co.uk but acually shows subdomain.mydomain.co.uk
How can I do this?
Try putting this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.co.uk [NC,OR]
RewriteRule ^(.*)$ http://subdomain.mydomain.co.uk/$1 [L,R=302,NC]
You could try doing an R=301, but beware of the dangers of using a 301 redirect. We had huge problems with our website at one point using 301 redirects as written about in this article: http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/

htaccess command to prevent master site access via subdirectory?

I have hosting setup with a master domain (mapped to the web root) and then a number of addon domains (each with their own folder within the web root). At the moment you can visit www.masterdomain.com/addondomainsubdir and reach the same page as you would if you visited www.addondomain.com (which maps to /public_html/addondomainsubdir). I want to prevent this so if you visit www.masterdomain.com/addondomainsubdir then it will do a 301 redirect to www.addondomain.com. The new addondomain.com site is a single page site so it does not have to map any additional pages.
Adding rules to the htaccess file in the web root does notaffect anything as the subdir exists which is wierd as i thought the htaccess command should work even if there is a matching subdir (i've tried the following which works when there's no matching subdir):
RewriteRule ^addondomainsubdir?$ http://www.addondomain.com [NC,R=301,L]
Logically given it's reaching this directory I figure i need to add a command within the htaccess file in the addondomainsubdir directory however nothing appears to have any effect (i've got various other rules setup and they work fine).
I would be massively grateful if anyone explain the best way to rectify this?
Thanks so much for your help,
Dave
I know this is an old post, but it has never been successfully answered. So for all of you finding this via search, this should do what the OP is asking.
Add this line to your .htaccess file:
redirect permanent /addondomainsubdir/ http://www.addondomain.com
Try these rules in your .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^([^/]+)/?$ http://www.$1.com/ [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^([^/]+)/?$ https://www.$1.com/ [R=301,L]
Instead of putting a rule in your main .htaccess, I would make make a .htaccess for each add-on domain, putting each one in the respective subdirectory.
RewriteEngine on
RewriteCond %{HTTP_HOST} masterdomain\.com$ [NC]
RewriteRule ^addondomainsubdir(.*)$ http://www.addondomain.com/$1 [R=301,L]

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources