Redirect another domain to folder without url changing htaccess - apache

I have a htaccess redirect (without url change) to a folder where my site is. Recently i got a new domain name, but i can't get the htaccess code working on that domain.
main domain: www.test.com
folder of site: /drupal
this is the htaccess code i use (so users go to www.test.com and see the /drupal conten)
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
RedirectMatch 301 ^/drupal/$ http://www.test.com/
Now, i have a new domain www.secondtest.com on that server, which content stands on /drupal2 but whenever i type www.secondtest.com into my browser he redirects me to www.test.com .
Is there a way to modify the htaccess like so?
www.test.com (go's invisible to folder) --> www.test.com/drupal
www.secondtest.com (go's invisible to folder) --> www.test.com/drupal2

You have to use an another RewriteCond to check the domain :
# Redirection if not www.test.com nor www.secondtest.com
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.secondtest\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
# redirection to drupal folder if test.com
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
# redirection to drupal2 folder if secondtest.com
RewriteCond %{HTTP_HOST} ^www\.secondtest\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal2/index.php?q=$0 [QSA]

Related

.htaccess redirect if domain = x.example.com

I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]

Laravel 4 htaccess Rewrite Rule with 301

I have following problem:
my website is available by two domains:
www.user.tucana.uberspace.de
and
www.my-domain.de
how is it possible to create a valid 301 Redirect from
www.user.tucana.uberspace.de to www.my-domain.de ?
I mean, if a user enter my webseite via www.user.tucana.uberspace.de/actual/news I want that my htaccess will redirect to www.my-domain.de/actual/news ...
In my htacces I create this code:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} !=www.my-domain.de
RewriteRule (.*) http://www.my-domain.de/$1 [R=301,L]
but the problem is ... if an user enter the site at www.user.tucana.uberspace.de/actual/news he will be redirect to www.my-domain.de/index.php/actual/news
This code will work, but not for the root URL:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !=www.my-domain.de [NC]
RewriteRule ^ http://www.my-domain.de%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
If I use the code above, the URL www.user.tucana.uberspace.de/actual/news change to www.my-domain.de/actual/news - this is right,
but if I type www.user.tucana.uberspace.de the URL will change to www.my-domain.de/index.php ... why :-( ?
So, how can I avoid the index.php in the url?
You can keep your rules like this:
Options -MultiViews
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.my-domain\.de$ [NC]
RewriteRule ^ http://www.my-domain.de%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Also test it out in a new browser to avoid 301 caching.

.htaccess - is rewriting one part of the url

Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [nc]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . url_handle.php
What I am trying to do is have a www rewrite rule so that urls without www. redirect to the same url with www. prefix. I am also using clean URL's, so any unknown URL is sent to url_handle.php for processing.
The problem is that if I have a url like "http://mysite.com/part_1/part_2/part_3", the URL is redirected and then reads "http://www.mysite.com/url_handle.php/part_2/part_3", ommitting the "part_1" and replacing it with "url_handle.php".
Firstly, to redirect non-www sites to www use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.mysite.com/$1 [L,R,NE]
It means, if the url is not "www.mysite.com" and if not blank redirect to "www.mysite.com"
Secondly, to redirect unknown sites to a specific page use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mysite.com/url_handle.php [R=301,L]
The last row will save you :)

Apache Rewrite : not matching pattern not working

I want to redirect visitors to my main domain when they perform requests on my subdomain followed by a not matching URI.
For example, a visitor can access a resource at sub.domain.com/product/10 but he should be redirected to domain.com when he attempt to access other resources that not match product/:id on my subdomain like sub.domain.com/anOtherResource.
I have to do this with apache rewrite module. I found that ! operator can do the job but it's not working for me.
Here is my Rewrite configuration from .htaccess file :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
I also tested this configuration :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule !^/product/[0-9]+$ http://www.domain.com [L,R]
I don't know where is the mistake ...
[Edit]
The .htaccess file is configured for Wordpress. Here is the entire .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The problem was in the Wordpress Rewrite configuration so what's happened ?
Step 1
I perform a request at sub.domain.com/product/1 so it not match these RewriteCond :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
Then, it not redirect to www.domain.com
Step 2
It continue the rewriting to the next Cond (Worpdress rewriting):
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
At this step, it send an Internal Redirect (not sent to the client) to sub.domain.com/index.php
Step 3
Because of the redirection, it apply again the previous RewriteCond
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
It match the Cond, then it redirect the client to www.domain.com while I didn't expect it.
The Solution
I fixed the problem by adding a RewriteCond on index.php as showing bellow :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
Now it manage the Internal Redirection sent by the Worpress Rewriting.
The rewrite rules look ok, but try the following:
RewriteCond %{HTTP_HOST} ^sub[.]domain[.]com$
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
If this doesn't work, then you might need to copy and paste the entire contents of your VirtualHost configuration since it could be something else causing a problem.

Apache .htaccess redirect if folder not present

I have a website where I need to do the following
if http://example.com/user1 does not exist, redirect the user to http://user1.example.com
Similarly http://www.example.com/user2 should redirect to http://user2.example.com
Is this possible using an .htaccess file?
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://%{HTTP_HOST} [R]
The above redirects to the home page, but how do I read the URL after / and do the redirect as above?
RewriteCond %{SERVER_NAME} =example.com # prevent infinite redirection
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://%1.%{SERVER_NAME} [R]
The somewhat cumbersome regex on REQUEST_URI ensures, only URLs of the http://example.com/user1 scheme will be redirected (as opposed to /user1/foo/bar).
Note the use of SERVER_NAME instead of HTTP_HOST (the latter is evil and should be avoided).
Try adding the following to the .htaccess file in the root directory of your example.com site.
RewriteEngine on
RewriteBase /
#if on example.com host
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
#and the directory/folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#redirect to folder.example.com
RewriteRule ^(\w+)$ http://$1.%{HTTP_HOST} [R,L]