Redirecting to a different directory - .htaccess

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]

Related

301 redirection whole domain to new one + few individual pages to new urls

I have olddomain.com - and I want to redirect to newdomain.com with htaccess and 301 - thats easy and working very well for me - if I am redirecting whole domain.
But on the new domain I changed few urls (now they are different then on the previous domain) and I want to redirect whole domain and few specific pages to few specific pages and I dont know how to combine this 2 conditions (redirect whole domain and redirect few specific pages).
This is working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]
and I would like to add to the code some specific redirects like this but I dont know how to combine it together that it will be working:
Redirect 301 /something/ https://newdomain.com/something-changed-new/
Thank you in advance for a help.
Check this rewrites in top of your .htaccess file
RewriteEngine On
RewriteRule ^something\/$ https://newdomain.com/something-changed-new/ [R=301,L]
RewriteRule ^other\/$ https://newdomain.com/something-changed-other/ [R=301,L]
RewriteRule ^old\/$ https://newdomain.com/new/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://newdomain.com/$1 [L]

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

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]

Redirect external subdirectory in .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

.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