.htaccess: mod-rewrite; subdomain - .htaccess

I am working on a new website bud I want to create a nice looking urls for my users. I don't know anything of htaccess and could not find my solution on google.
I need to link:
user.mywebsite.com to>> mywebsite.com/users/user
user2.mywebsite.com/contact to>> mywebsite.com/users/user2/contact
Some links may not be linked like:
www.mywebsite.com may not link to mywebsite.com/users/www
Is this possibe to do with htaccess?
If yes can someone explain it to me?

Using mod_rewrite, you can try:
RewriteEngine On
# the request URI doesn't already start with /users/
RewriteCond %{REQUEST_URI} !^/users/
# host doesn't start with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# host starts with something else
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mywebsite\.com$ [NC]
# rewrite
RewriteRule ^(.*)$ /users/%1/$1 [L]
This will make it so when someone enters http://joe.mywebsite.com/some/page.html they will be served the file in /users/joe/some/page.html

Redirecting your old links to new ones.
Redirect user.mywebsite.com mywebsite.com/users/user
Redirect user2.mywebsite.com/contact mywebsite.com/users/user2/contact

You can take a look at VirtualDocumentRoot
e.g. the documents for www.user.isp.com are found in /home/user/.
# include part of the server name in the filenames
VirtualDocumentRoot /www/hosts/%2/docs
# single cgi-bin directory
ScriptAlias /cgi-bin/ /www/std-cgi/

Related

.htaccess forward from one domain to a specific .html page

I know how to use .htaccess to forward everything in one domain to a new domain name. But in this case, I want everything from one domain to go to a specific .html page on a different domain. That's where I'm lost. I'm trying the following but it just redirects to a folder and the page in question is in that folder but obviously, I don't want people seeing the contents of that folder. Make any sense? So example.com needs to go to yyy.com/some-page.html
This is what I'm currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule ^(.*)$ http://www.1.yyy.com/$1 [R=301,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule .* http://www.1.yyy.com/some-page.html [R,L]
You can also put a blank index.html page to the directory in question to mask its contents or
you can put index.php file with this code <? header ("location: http://www.1.yyy.com/some-page.html"); ?> that will redirect a user to the desired page.
$1 is a place holder for the 1st pattern match. So if you are rewriting domaina.com/someurl/, it is attempting to load domainb.com/someurl/. Swap the $1 with the actual page --- e.g. somepage.html and it should work. But unless both of these domains are pointing to the same files/directories, the rule seems a bit overcomplicated.
So how about just a simple redirect?
Try this in your .htaccess file.
redirect 301 / http://somesite.com/somepage.html
OR you can try this.
RewriteRule ^(.*)$ http://somesite.com/somepage.html [R=301,L]
It does work and you can test my RewriteRule below.
http://htaccess.madewithlove.be/
There must be something else going on.

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]

Virtual subdomain with wildcard mapping .htaccess

I am building a download site, An users will be able to register and a folder with their username will be created on my server, something like: home/users/username
What I want to accomplish is, if anyone types: username.domain.com in their browser, it will route them to: home/users/username/, and if they type: username.domain.com/file.mp3, it will route them to: home/users/username/file.mp3
If its possible to accomplish sub folders routing, that would be great full aswell, example; home/users/username/sub/file.mp3
Thanks guys
Try adding the following to the .htaccess file in the root directory of your site.
This will rewrite any request to username.domain.com to the correct folder/subfolder and file.
I am assuming that home is a directory in the root folder of your site.
RewriteEngine on
RewriteBase /
#if request for usename.domain.com/anything
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
#send any request that is not already for home/users to home/users/username/anything
RewriteRule ^(?!home/users/) home/users/%1%{REQUEST_URI} [L,NC]
I tried what Ulrich Palha recommended but no luck. I hope this will help guys like me where Ulrich's answer doesn't work.
# Rewrite username.domain.com/<path> to domain.com/home/username/<path>
#
# Rewrite only if not already rewritten to /home/
RewriteCond $1 !home/
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
# Rewrite to /home/username/URL-path
RewriteRule (.*) /home/%1/$1 [L]

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]

htaccess command to prevent master site access via subdirectory?

I have hosting setup with a master domain (mapped to the web root) and then a number of addon domains (each with their own folder within the web root). At the moment you can visit www.masterdomain.com/addondomainsubdir and reach the same page as you would if you visited www.addondomain.com (which maps to /public_html/addondomainsubdir). I want to prevent this so if you visit www.masterdomain.com/addondomainsubdir then it will do a 301 redirect to www.addondomain.com. The new addondomain.com site is a single page site so it does not have to map any additional pages.
Adding rules to the htaccess file in the web root does notaffect anything as the subdir exists which is wierd as i thought the htaccess command should work even if there is a matching subdir (i've tried the following which works when there's no matching subdir):
RewriteRule ^addondomainsubdir?$ http://www.addondomain.com [NC,R=301,L]
Logically given it's reaching this directory I figure i need to add a command within the htaccess file in the addondomainsubdir directory however nothing appears to have any effect (i've got various other rules setup and they work fine).
I would be massively grateful if anyone explain the best way to rectify this?
Thanks so much for your help,
Dave
I know this is an old post, but it has never been successfully answered. So for all of you finding this via search, this should do what the OP is asking.
Add this line to your .htaccess file:
redirect permanent /addondomainsubdir/ http://www.addondomain.com
Try these rules in your .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^([^/]+)/?$ http://www.$1.com/ [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^([^/]+)/?$ https://www.$1.com/ [R=301,L]
Instead of putting a rule in your main .htaccess, I would make make a .htaccess for each add-on domain, putting each one in the respective subdirectory.
RewriteEngine on
RewriteCond %{HTTP_HOST} masterdomain\.com$ [NC]
RewriteRule ^addondomainsubdir(.*)$ http://www.addondomain.com/$1 [R=301,L]

Resources