htaccess redirect entire domain/folder to another domain - apache

I am not a friend of htaccess and have bumped into a problem I can't seem to google a solution for. So here is hoping that you guys can help.
I have a multisite instalation where I am trying to redirect all trafic to mydomain.com/anothersite (and all sub folders and query strings) to anothersite.com and at the same time have all requests to anothersite.com rewrite to requesting the server to look up the anothersite folder.
Basically the server folder setup is as follows:
in the root we have a website
in the root we have a folder "anothersite" that contains another site
So all requests to mydomain.com should show the root folder while all requests to anothersite.com should get rewritten.
Hope that made sence
Here is what I have (it almost works but not quite)
RewriteEngine on
RewriteBase /
RewriteRule ^mydomain\.com/anothersite(.*)$ http://anothersite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule !^anothersite/ anothersite%{REQUEST_URI} [L]
problems are that requests to mydomain.com/anothersite/someFolderOrQuerystring isn't redirected, while mydomain.com/anothersite/ is
Hope it made sence
(btw: if someone can point me in the direction of a book, that will make me a master of the mysteries of the htaccess file that would be a sweet bonus

Have this rule in site root .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^anothersite(/.*)?$ http://anothersite.com%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?anothersite\.com$ [NC]
RewriteRule !^anothersite/ anothersite%{REQUEST_URI} [L,NC]
Make sure you don't have any .htaccess under anothersite/ folder.

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

Which way do htaccess rules cascade?

I'm trying to archive an old website behind a /v4 directory on the new website. I was hoping that the below .htaccess was going to redirect any page from the example website to its corresponding page archived under the new domain. However, when I just tested the site it appears that example.org.au/contact.asp just went to newdomain.com/v4/ instead of `newdomain.com/v4/contact.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.org\.au$
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule (.*) https://newdomain.com/v4/ [R=302,NC,L]
The second RewriteRule is there for when people just go to the root domain but is it overriding everything?
Do it like this, and also your first rule was set to ignore example.org.au so would never fire. This will process any host served by the site. If that's not what you want let me know:
RewriteEngine On
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule ^$ https://newdomain.com/v4/ [R=302,L]

HTAccess: Internal redirect directories (or symlink?)

I'm currently struggelling with the following situation which I want to solve with HTacess redirection.
The main CMS is running in the root directory /.
In the subdirectory /shop/.. there is a shop (based on another system)
Till now everything is fine: The main website is rewritten by /.htaccess and the shop by /shop/.htaccess
Now I want to access an english version of the shop with /en/shop/..
Actually the calls should only be redirected to the shop-system in the directory /shop (but with the url /en/shop/.. in the browser)
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/en/shop/$
RewriteRule ^(.*)$ /shop/$1 [L,QSA]
Which is not really working.
My question is: Can I solve that with simple htaccess or would it be a more elegant solution to make a symlink /en/shop => /shop ?
(Are there any performance differences between these solutions?)
Thanks in advance
Put the following code at the root folder .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+en/ [NC]
RewriteRule ^shop/ en%{REQUEST_URI} [R=302,L,NE]
RewriteRule ^en/(.*)$ $1 [L,NC]

How to restrict other websites from accessing by htaccess

Recently I have encounter a strange issue , my website www.xyz.com is being pointed by some one on the web domain let suppose www.abc.com.
Though the whole website is on www.xyz.com but the other domain display every single content and directory path structure by their domain...e.g. the real path is www.xyz.com/somepage/id/etc can be work by www.abc.com/somepage/id/etc with same directory paths....
This other website is just redirecting everything to my website and I want to stop this domain to use my directory structure. This www.abc.com is also being crawled by Google crawler and added its link in Google search engine.
This is a very new issue to me I have one solution to restrict every single request and check if its from my own website or not.
Second solution is to restrict them through htaccess but I don't find perfect solution using htaccess.
I saw on the web it stop all the referrer, but doing that I am afraid if it will stop users coming from other website to my website ...I just need to restrict other domains to use my whole website as theirs using redirection...i have taken this issue on go daddy and they said they also don't know why the other website is pointing to my ip address ... so clueless I need expert advice to secure my website from future issues like this ...kindly advice...
My htaccess is
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
and i am using YII...
You can place this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REMOTE_HOST} abc\.com$ [NC,OR]
RewriteCond %{HTTP_REFERER} abc\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !xyz\.(com|net)$ [NC]
RewriteRule ^ - [F]
In your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.xyz\.com$
RewriteCond %{HTTP_HOST} !^subdomain\.xyz\.com$
RewriteRule .* - [F]