Redirect external subdirectory in .htaccess - .htaccess

The following code works perfectly to redirect an external URL to a subdirectory on my site:
RewriteCond %{http_referer} abc\.com [NC]
RewriteRule ^$ my-subdirectory/ [R=302,L]
This code tells anyone navigating from abc.com to my website to be redirected to the subdirectory listed (e.g. mywebsite.com/my-subdirectory).
But what I really want is to redirect a referring subdirectory. in other words: abc.com/some-subdirectory. How do I add "some-subdirectory" to the first line of the above code to have abc.com/some-subdirectory reroute to mywebsite.com/my-subdirectory?

Try:
RewriteCond %{http_referer} abc\.com/some-subdirectory [NC]

I think you need something like this :
RewriteRule ^subdirectory/(.*)$ /anotherdirectory/$1 [R=301,NC,L]
And look at here

Related

Redirecting to a different directory

I have a full site setup at mysite.com/2/
I want urls like:
mysite.com/about to redirect to mysite.com/2/about
mysite.com/work/photos to redirect to mysite.com/2/work/photos
I was hoping I could solve this and add the /2 after the domain through the htaccess file and not have to move the whole site up a level.
Try the folowing code if you want redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} !(about|work/photos)/(2)
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [R=301,L]
If you need only internal redirection change the last line with this :
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [L]
If you want to change all requests :
RewriteEngine On
RewriteCond %{REQUEST_URI} !/(2)
RewriteRule ^(.*)/(.*)$ /$1/2/$2 [R=301,L]

Point domain to subdomain folder with same url

I have a domain
http://www.example.biz/
I developed a site and put the code in a sub folder on this domain for example, the site is at
http://www.example.biz/site
now, I wanted to point my main domain to this folder so that when user visits example.biz he actually sees example.biz/site and for that, I added the below lines in .htaccess
RedirectMatch ^/$ /site/
it works perfectly however, the url user sees is
http://www.example.biz/site
I do not want that, I wanted user to only see http://www.example.biz as URL but this domain should point to the sub folder invisibly. How should I do that ?
Using mod_rewrite you can use the below rule, now when you access http://www.example.biz it will show the index file from for site/.
RewriteEngine On
RewriteRule ^$ /site/ [L]
Or if you want to show every file try with below,
RewriteEngine On
RewriteRule ^(/|.+)$ site/$1 [L]
I found a simpler solution,
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.biz$
RewriteRule !^site/ /site%{REQUEST_URI} [L]

301 redirect specific URLS if ?noRedirect not in URL

I'm running a wordpress installation and want to move specific feeds off-site. I've already got most of the technology down, but here's the problem. I want the following URL:
http://www.csicon.net/g/feed
to redirect to
http://feed.mesr.it/g
But if the URL comes in like this:
http://www.csicon.net/g/feed?noRedirect
I don't want it to redirect but load the original. Any thoughts?
The .htaccess file on http://www.csicon.net/ would contain:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\/g\/feed$ http://feed.mesr.it/g [L,R=301]
Note: It has not been tested, but you get the idea.
Later Edit: Also, this can be done in PHP.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)csicon\.net$ [NC]
RewriteCond %{QUERY_STRING} !(^|&)noRedirect [NC]
RewriteRule ^([^/]+)/feed/?$ http://feed.mesr.it/$1 [L,NC,R=302]

Redirect only the start page to extern site all sub categories should stay

I have a problem were I need to redirect www.mydomain.com to an external url www.otherdomain.com but I need all the subpages and urls to stay as they are (not get redirected).
Example www.mydomain.com/testpage should not get redirected. Is this possible?
And if so can anyone guide me what I need to write in my htaccess file for it to work.
The following .htaccess will do the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.\mydomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.otherdomain.com/ [L,R=302]
It will redirect only the base domain (root), all subfolders and other URLs will stay.
Reference:
https://serverfault.com/questions/58762/how-to-redirect-root-and-only-root-via-htaccess

.htaccess Redirections

I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.
Basically, I want a solution that will take a site example.com and allow you to enter URL's like:
123.example.com
ksdfkjds.example.com
dsf38jif348.example.com
and this would redirect them to:
example.com/123
example.com/ksdfkjds
example.com/dsf38jif348
So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain.
Try something like this:
# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com
# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo
Change the last line if that's not what you want.
If you just want them to be the entrance:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]
Otherwise:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]

Resources