.htaccess redirect a specific directory - .htaccess

I am in need of help with build an htaccess file. Basically I am moving an website from www.abc.com to www.xyz.com. Now, here is the two things that I am trying to achieve:
All users should be redirected to www.xyz.com when accessing www.abc.com
If user accesses www.abc.com/files/abcd.file or www.abc.com/files/folder/abcd.file, etc. should be redirected to arhive.xyz.com/abcd.file, etc. Basicly, this URL www.abc.com/files/, should be replaced with arhive.xyz.com, keeping the same file/directory structure.
Someone can help? Thanks in advance.

You have several requirements, but if I understand correctly this should get what you need in the htaccess file in the root directory.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} !^/files(/.*)?$ [NC]
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/files(/.*)?$ [NC]
RewriteRule ^ http://archive.newsite.com%{REQUEST_URI} [R=301,L]
Of course change to your domain names.

Related

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

Domain redirection with folder separation. htaccess

After redirecting my client domain to my server i created rewrite rule in root folder .htaccess file to point domain to a subfolder1 (joomla website):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/subfolder/
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Everything worked fine but today i found out that i can access any other subfolder in my server by entering for example: myclientdomain.com/subfolder2 and what's worse google can index that and show it in search results.
If there is any way to redirect a domain in a way that I won't be able to access any other folder on my server?
I would really appreciate help as I searched throughout google for answer, my server tech support said that they don't really support these kind of problems (they only gave me a piece of code from above) and I don't really know anything about .htaccess rules and how it works.
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder\/?(.*)?$
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Change above rule to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
# if current URI is not starting with /subfolder/ then route to /subfolder/
RewriteRule ^((?!subfolder1/).*)$ subfolder1/$1 [L,NC]

Cannot 301 Redirect Addon Domain

.
I have the current setup:
primarysite.com/addondomain1.net
primarysite.com/addondomain2.net
primarysite.com/addondomain3.net
.
Here is what I want to accomplish:
I would like anyone (or spiders) who try to access primarysite.com/addondomain1.net to be served addondomain1.net instead.
For example, if someone were to attempt to access primarysite.com/addondomain1.net/about.php, they would instead be served addondomain1.net/about.php
.
The Problem:
I have tried to set the .htaccess file for each subdirectory of primarysite.com but it just doesn't work correctly. Here is my current settings placed within each addon domain:
RewriteCond %{HTTP_HOST} ^(www\.)?primarysite\.com$ [NC]
RewriteRule ^ "http://addondomain1.net/" [L,R=301]
.
I appreciate your help. I would gladly donate some BTC to the user who can solve this problem
Try:
RewriteCond %{HTTP_HOST} ^(www\.)?primarysite\.com$ [NC]
RewriteRule ^addondomain1\.net(.*)$ http://addondomain1.net$1 [L,R=301]
you'll need to do that for each of your add-ons.
You can place this rule in each sub-directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?primarysite\.com$ [NC]
RewriteRule (.*) http://addondomain1.net/$1 [L,R=301,NE]

How do I redirect a subdomain to an HTML file?

I've been trying out all sorts of examples to redirect subdomains to files on my server.
So, let's say someone went to apple.domain.com.
Using htaccess, I want it to redirect to domain.com/apple.html
Here's the rules I have right now, which don't work.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteRule ^ /%1.html [L]
Thanks so much in advance for the help.

Redirecting dynamic subdomain with directory structure to a single file

I have a domain name with wildcard dns active. what i want to write in htaccess is that when the user type abc.domain.com/news/news-details.php then it should send request to the file news.php with arguments val=abc.
I have in the htaccess the following code:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com
RewriteCond %{REQUEST_URI} ^news/news-details\.php$
#RewriteCond %{QUERY_STRING} !^id=.
RewriteRule (.*) /news.php?div=%1 [L]
But this is throwing a 404 error. what is the core part are the following two lines to explain
request for www.domain.com/news/news-details.php goes to www.domain.com/news.php
and abc.domain.com/news/news-details.php goes like www.domain.com/news.php?val=abc
Please note that i am not redirecting. the urls will show as a dynamic sub domain. physically there will be no folder. all files are located in the root.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule ^news/news-details.php$ /news.php?div=%1 [NC,QSA,L]

Resources