How can I change all subdomains to read different content from their respective subdirectories? - apache

I have my DNS configured to accept any subdomain (wildcard *), but I am having trouble feeding back the required content to the browsers.
I would like each subdomain to return the relative content, which resides in subdirectories of the same name within the public_html path of my server.
eg, example.domain.com/picture.jpg would actually request the file at public_html/example/picture.jpg
Currently I have tested the followed .htaccess code, but it is not functional:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
This code, and similar tests, can redirect based on the subdomain (%1) fine, but the request string ($1) seems to be the issue.

Maybe you could take a look at the mod_vhost_alias module :
http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html

Try the following:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.com/%1/$1 [L]
I didn't test this one but I use similar rules for proxying:
RewriteCond %{HTTP_HOST} !^domain.local [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.local/%1/$1 [P]

Related

Pass parameters to PHP on subdomain with htaccess

So I have a domain (example.com) with WordPress running on it and a subdomain (u.example.com) that does not. The files for the subdomain are stored at example.com/u. I am trying to pass parameters to my subdomain using slashes. For example: u.example.com/sometext would be processed by u.example.com/index.php?par=sometext. I have been searching and trying different things, but I cannot get it to work with the subdomain.
Here is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^u\.example\.com$ [NC]
RewriteRule ^/?(.*)\/? u\/index.php?par=$1 [R=301,L]
I was able to solve my problem using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^u\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^/?(.*)\/? \/index.php?par=$1 [R=301,L]
Originally it would send me into an infinite loop so RewriteCond %{REQUEST_URI} !index.php was added to prevent that.

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

Redirecting old domian to new domain by .htaccess

I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess file.
we have almost same structure of the new domain which includes
URL's
Database
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?
The Redirect directive won't be bound to the RewriteCond conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] rule gets applied before the Redirect directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
Where the /path/to/file/map.txt will look something like:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]

RewriteRule help

I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.