.htaccess redirect from directory to subdomain without changing url - apache

I would like to redirect /blog/ to another project. Without changing the url.
The hole thing almost works with the redirect rules below and using Apache mod_proxy, but only if it hits the index itself on /blog/ it DOES redirect to blog.website.nl.
RewriteCond %{HTTP_HOST} ^website\.nl [NC]
RewriteRule ^(.*)$ http://blog.website.nl/$1 [P,L]
When I visit website.nl/blog/category/test/
it works fine!
So why is this still changing the url on /blog/ and how to fix that?
Thanks! This has been quite a puzzle for me so far.
Update:
I managed to get it working. Instead of adding a .htaccess file to blog folder I added this code to the root /htaccess file. Now it all works!
RewriteCond %{HTTP_HOST} ^website\.nl[NC]
RewriteRule ^/?blog/(.*)$ http://blog.website.nl/$1 [P,L]

Related

Redirecting All Requests to Index.html

I would like to redirect all requests made to my website to index.html if the request aren't made from AJAX. Apart from just redirecting, I would like to append the request URI as a get parameter to index.html, for example if someone visits http://example.com/something.html, the person should be redirected to http://example.com/?origin=something.html. To achieve this, I have the following code in a .htaccess file
RewriteEngine On
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_URI} !^/(index.html)?$ [NC]
RewriteRule ^/.*.html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]
However, it does not redirect any requests as expected. I'm on Apache/2.4.33, Ubuntu 16.04. What am I doing wrong?
As CBroe pointed out, I was facing the issue because the context of .htaccess has the initial / removed from %{REQUEST_URI} that is usually the conditional part of RewriteRule. Therefore, the solution was only having to change the RewriteRule to:
RewriteRule ^(.*).html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

htaccess redirect entire domain/folder to another domain

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.

htaccess adding WWW and changing filename in subdirectory

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!
I have an .htaccess file in my root directory adding "www" to everything:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]
Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.
I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.
Any ideas?
You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]
Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.
I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]
So that it redirects first, then the inherited rule (the www) gets applied after.
You could also just put both rules in the same file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]

.htaccess mod_rewrite redirect without query string variables not working

I'm having a problem with one of my rewrite rules. I would like to redirect all of the following URL's to another URL without the query string.
/gallery/products.aspx?C=9&SC=&ID=428&P=10
/gallery/products.aspx?C=2&SC=2&ID=128&P=1
/gallery/products.aspx?ID=147&C=2&SC=&P=7
/gallery/products.aspx?ID=1337&C=15&SC=&P=1
/gallery/products.aspx?ID=1532&C=3&SC=&P=2
/gallery/products.aspx?C=9&SC=&ID=1489&P=1
/gallery/products.aspx?C=7&SC=&ID=100&P=2
/gallery/products.aspx?C=2
/gallery/products.aspx?ID=1328&C=14&SC=11&P=17
/gallery/products.aspx?C=1&SC=&ID=767&P=3
/gallery/products.aspx?ID=1270&C=1&SC=&P=26
and I have this in my .htaccess file
RewriteRule ^gallery/products.aspx http://www.domain.com/category/? [L,R=301]
but it's not working. I checked it in a .htaccess simulator and it found the rule then redirected, but when I upload to my server, it doesn't redirect. I've also tried some other rules with no luck
I was finally able to make this work with the following:
RewriteCond %{HTTP_HOST} www.domain.com [NC]
RewriteRule products.aspx http://www.domain.com/category? [L,R=301]