I have my directory like this:
/index.php (website main page)
/sites
site1
site2
site3
when I go to site1.example.com, i want it to show example.com/sites/site1/index.php
when I go to site1.example.com/page1, i want it to show the file example.com/sites/site1/page1.php
how can i do that? thanks!
I assume all your subdomains point to the same place as the main domain, so subdomain.example.com and example.com point to the same place.
Try something like the following in the .htaccess file in the root of the main site:
RewriteEngine On
# Exclude requests for the www subdomain
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ - [L]
# Requests for the site root
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule ^$ sites/%1/index.php [L]
# Requests that are "assumed" to be .php files
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule ^([^.]*)$ sites/%1/$1.php [L]
# All other requests
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule !^sites/ sites/%1%{REQUEST_URI} [L]
This assumes that a valid URL-path does not contain a dot.
%1 is a backreference to the captured group in the CondPattern (ie. the subdomain) and $1 is a backreference to the captured group in the RewriteRule pattern.
However, how do you want to handle non-PHP files and other static resources?
Also, how can I do a 404 page?
You can define a custom error document. For example:
ErrorDocument 404 /errors/404.php
However, you'll need to make an exception at the top of your .htaccess file to prevent this from being rewritten by the directives that follow. For example:
RewriteRule ^errors/ - [L]
You need to:
Setup a Wildcard DNS record
Write a rule for your .htaccess
The rule could look like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/sites/%1/$1 [L,NC,QSA]
That's it.
Related
enter code here Hi I've the following code inside my htaccess file.
My wildcard subdomain routes to "mainfolder" - here i placed the htaccess file.
I've the following folders
"mainfolder"
"mainfolder/sub1"
"mainfolder/sub2"
etc.
Calling the subdomain - sub1.domain.com it should route to the subfolder "sub1" (subfolder=subdomain).
I tried to do it with this code
#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]
#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$ [NC]
RewriteRule ^(.*) %1/$1 [L]
The first rule works well, but if I add the second rule I receive a internal server error.
Whats wrong with this - how how I can change the first rule in this way, that it works with all url-parameters after .com - that was the reasons for me to add the second rule.
Hope I get help for this. thanks a lot.
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
LIke
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
RewriteCond %{REQUEST_URI} !^sub1/
RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
If more understanding step wise then follow https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/
My current .htaccess is in httpdocs.
How can I configure it to help domain point to subfolder is /vn/
and still keep: www.example.com instead of www.example.com/vn/
Try the following near the top of your .htaccess file in the document root:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /vn/$1 [L]
This internally rewrites all requests that hit the document root to the /vn/ subdirectory. The RewriteCond directive that checks against the REDIRECT_STATUS environment variable ensures the request is only rewritten once - thus avoiding a rewrite loop.
In .htaccess add these lines :
RewriteEngine On
RewriteRule !^blog/ /blog%{REQUEST_URI} [L,R=301]
Or in some hosts :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomainName.com$
RewriteRule ^(/)?$ blog [L]
I have one project online and it on two diffrent domains 1)www.example.com and 2)www.example.co.in. now, I want to redirect
www.example.co.in/category/
to
www.example.com/ceramic+industry/
And I want to redirect it through .htaccess or from server. I tried from server that only redirect domain name not directory, and also tried from .htaccess where I can redirect only domain or only directory not when both domain and directory combine. My project in php. So, you can give advise if it's possible in php also.
Add following rule in .htaccess(placed at root of website www.example.co.in):
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
or use 302 for temporary redirects.
IF you also want to redirect whole .co.in website to .com THEN add next line(remember in .com website's .htaccess these two Rules should not be there.)
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
If for any reason you are bound to use same .htaccess on both site then use condition like this:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
============================================================
Solution to your another query:
This can be done by PHP. As I guessed all URL after category/ is handled by one PHP page.
in that PHP page code this at top:
if(isset($_GET['company'])){
$company=$_GET['company'];
$companyNew=str_replace('_','+',$company);
if($company!=$companyNew){
header("location:/category/?company="+$companyNew,true,301);
//header("location:/ceramic+industry/?company="+$companyNew,true,301);
exit;
}
}
You can try this:
RewriteCond %{HTTP_HOST} ^your.first.domain$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://your.second.domain/$1 [R,L]
This will redirect, if your request contains existing directory name.
Answer to your comment. You can try this:
RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^company=(.*)\_(.*)$
RewriteRule ^(.*)$ /$1?company=%1+%2 [R,L,E=FINISH:1]
First two lines allow you to prevent infinite loop redirect.
We are using a wildcard subdomain, *.subdomain.com Each state has its own directory inside the /state/ directory, and within that directory there is a state.html. The URL would be domain.com/state/$state/state.html which would then be rewritten to show domain.com/state.html?state=$state
All works good but now we need to go from subfolder to subdomain so the ending result would be either
$state.domain.com - Preferred but not necessary
or
$state.domain.com/state.html?state=$state
What we have currently is below, but we get a redirect loop.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1 [P,L,QSA]
#Rewrite Rule - Force state and city lookup to use state.html or city.html and use info from url to define which city and state.
RewriteRule ^state/(.*)$ state.html?state=$1 [L]
RewriteRule ^city/(.*)/(.*)$ city.html?city=$1&state=$2 [L]
Ended up using the following which works
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1/state.html?state=$1 [L,QSA]
How can I use RewriteRule to point the website http root to a sub folder?
I always use the rule below but now after upgrading to the latest wamp server, it does not working anymore. Why?
RewriteRule ^/?$ local/applications/bin/oldsite/index.htm [L,QSA]
I want to point, for instance,
http://mysite.com/ to http://mysite.com/local/applications/bin/oldsite/index.htm
or in localhost wamp server, it should be like this,
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
Any ideas?
And also,
http://localhost/mysite/contact.htm to
http://localhost/mysite/local/applications/bin/oldsite/contact.htm
http://localhost/mysite/about.htm to
http://localhost/mysite/local/applications/bin/oldsite/about.htm
EDIT
I managed to point the other pages to the specific locations with the rule below,
RewriteRule ^([a-zA-Z0-9\-]+)\.htm/?$ local/applications/bin/oldsite/$1.htm [L,QSA]
But still I cannot point
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
UPDATED
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#### localhost sets of rules
# No target file, go to index.php
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/?$
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteRule . mysite/local/applications/bin/oldsite/index.htm [R=301,L]
# Target file, go to file
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/([^\.]+)\.htm [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . mysite/local/applications/bin/oldsite/%1.htm [R=301,L]
#### Live site set of rules
RewriteCond %{HTTP_HOST} (?:www\.)?mysite\.com [NC]
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule . local/applications/bin/oldsite/index.htm [R=301,L]
The first sets of rules are for the local host and redirect permanently:
http://localhost/mysite/filename.htm
To:
http://localhost/mysite/local/applications/bin/oldsite/filename.htm
When filename.htm is not present in the incoming URL, filename.htm in the substitution URL is always index.htm
The string mysite is assumed to be a fixed string while filename is assumed to be a variable.
The second rule-set is for the web server and redirects permanently only
http://mysite.com/
To:
http://mysite.com/local/applications/bin/oldsite/index.htm
In the last case, if the incoming URL holds any additional path the rule is not applied.
For silent mapping, remove R=301 from [R=301,L].
Copy the above sets of rules into both .htaccess files in the local and remote root directories.
This might get you started (untested):
Check in a condition whether the request does not point to a particular subfolder. If so, redirect the request to a subfolder. You can use in the target if you want. Instead of the HTTP Status 301 you can use another one if more appropriate.
RewriteCond %{REQUEST_URI} !^/(local/applications/bin/oldsite/.*)$
RewriteRule ^(.*)$ http....n/subfolder/%1 [R=301,L]
It seems like you want to make the "oldsite" directory your Document Root - right?