Redirecting Subdomain to Subfolder - .htaccess

I try to add the following logic to a .htaccess file:
subdomain.domain.xx make a redirect to subdomain.domain.xx/subdomain/
xy.domain.xy redirect to xy.domain.xy/xy/ (it should work with every subdomain without adding new rewrites)
i found a lot of solutions to redirect subdomain.domain.xx -> domain.xx/subdomain, but nothing like i need... below you find the code i try out.
Hope someone can help me. Thanks.
RewriteEngine on
RewriteBase /
RewriteRule ^([^.?]+[^.?/])$ $1/ [R]
RewriteCond %{HTTP_HOST} ^(www)?.domain.ch/$
RewriteRule .* http\://%1$1.domain.ch/%1$1 [I,R=301]

Place this in the .htaccess file of your subdomain's root directory:
DirectoryIndex index.html
RewriteEngine on
Redirect permanent /index.html http://subdomain.domain.xx/subdomain/

Related

How to change the route in HTACCESS

I have a web route problem in which one is badly located and I want to change it or redirect the one I want with the .htaccess file of my web page. Try creating a RewriteRule but it did not work. Could you help me?
Erroneous route: mysite.com/swf/c_images/navigator-thumbnail/foto.jpg Route that I want you to address or change: mysite.com/photos/thumbnail/foto
Here is my RewriteRule
RewriteRule ^swf/c_images/navigator-thumbnail/?$ $1/photos/thumbnail$2 [R=301,L]
Check this rule in top of your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^swf\/c_images\/navigator-thumbnail\/(.*)\.jpg$ /photos/thumbnail/$1 [R=301,L]
</IfModule>
Rule rewrite your link
mysite.com/swf/c_images/navigator-thumbnail/foto.jpg <> mysite.com/photos/thumbnail/foto
and all jpg foto in /swf/c_images/navigator-thumbnail/ to /photos/thumbnail/
mysite.com/swf/c_images/navigator-thumbnail/any-foto.jpg <> mysite.com/photos/thumbnail/any-foto

Rewrite URL to get contents from sub-directory

I have edited my /etc/hosts file to link domain.com to my localhost. Now when I visited domain.com I want it to get the site contents from domain.com/site/ but only using the URL domain.com thus removing site/ from the URL.
I have done this once before, so I know it's possible, but I lost the code. Can someone please assist me? .htaccess scripting is not my strong. Apologies if this is a duplicate of something else.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!site/).*)$ site/$1 [L,NC]
I have managed to get this working with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)your_directory
RewriteRule ^(.*)$ your_directory/$1 [L]

subdomain including htaccess redirect

I have a subdomain code.domain.com that redirects to domain.com/code
I want to redirect for example: code.domain.com/hello/world.html to domain.com/code/?path=hello/world.html
This is what I have so far:
RewriteEngine ON
RewriteRule ^code/(.+)$ http://domain.com/code/?path=$1
But I am not sure what is wrong with it, it doesn't seem to work.
Thanks.
Use these rules in the root directory htaccess file (not the code directory) (you can also point the code.domain.com to the root directory instead if you want) :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^code.domain.com$
RewriteRule ^(.+)$ /code/?path=$1

htaccess URL rewrite rule for everything under a folder

I have looked but can't find anything that works. I have an old site that we have updated. They had everything under a folder called site under the root. Now all the customers who have this bookmarked I would like to redirect them, regardless of what is after the folder site (subfolders, files), to the main page of the new site instead of page not found on our new WordPress install. Any help appreciated.
Old URL: http://www.oldsite.com/site/.... to new URL http://www.newsite.com
I have tried this to no avail
Rewrite Rule ^site/(.*)$ http://www.newsite.com
Thanks.
try:
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite.com$ [NC]
RewriteRule ^site/(.*)$ http://www.newsite.com/$1 [R=301,L]
This redirects something like http://www.oldsite.com/site/some-page.html to http://www.newsite.com/some-page.html (the matching bit of the URI after /site/ gets carried over in the 301 redirect), but if you want to redirect everything for /site/ to the index root of newsite, replace the target in the RewriteRule to http://www.newsite.com/ (remove the $1 bit).
EDIT:
I actually write it wrong above. It is actually the same domain name. The question should read old URL mysite.com/site.... everything under this folder to just redirect to mysite.com
Then what you want is:
RewriteEngine On
RewriteRule ^site/(.*)$ /$1 [R=301,L]
Or alternatively with mod_alias:
RedirectMatch 301 ^/site/(.*)$ /$1
Looks like all you need is this simple 1 liner rule:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^site/ / [R=301,L,NC]

Create a 301 redirect in .htaccess for folder to subdomain AND redirect www to non-www

I've searched everywhere and used some examples but they don't meet my specific needs, hence why I'm asking here if anyone can please help? I know the http:: below isn't correct, it's just because I can't post links here.
I'd like to redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
whilst also being able to redirect
http:://www.mysite.co.uk/mysub/
and 2. http:://mysite.co.uk/mysub/
to 3. http:://mysub.mysite.co.uk/
All files in 1. and 2. should be redirected to their equivelant in 3.
For example: http:://www.mysite.co.uk/mysub/file.html AND http:://mysite.co.uk/mysub/file.html should both go to http:://mysub.mysite.co.uk/file.html
This is to affect the subdomain only, so that other folders and files in the site root aren't affected.
If anyone could help me understand and write the code for the 301 redirect in a .htaccess file I'd really really appreciate it! Thanks!
This is the code you'll need in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]

Resources