Pointing the website http root to a sub folder with htaccess? - php

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?

Related

Catching the domain name with .htaccess

I'm quite new in using .htaccess and modrewrite.
I have a multitenant website in php and every tenant can save a domain name he bought in the app database after pointing it to my server ip.
Now I'm trying to write a rewrite Rule via .htaccess so that when the user types his domain in the addressbar (e.g. www.example.com) the .htaccess parses the hostname and passes it as a parameter to an index page, so that every request done to www.example.com becomes something like that: /index.php?domain=www.example.com .
And even www.example.com/user to become /index.php?domain=www.example.com&url=user
How can I do it with .htaccess?
Thank you for your help !
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !domain= [NC]
RewriteRule ^(?:index\.php)?$ index.php?domain=%{HTTP_HOST} [L,QSA,NC]
RewriteCond %{QUERY_STRING} !domain= [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?domain=%{HTTP_HOST}&url=%{REQUEST_URI} [L,QSA]

htaccess routing with url parameters

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/

How to avoid the redirection to index.php when trying to access non www urls

I have a Laravel website in production and here is my problem explained with 4 simple url tests:
Access: https://www.my-website.kr/
Result: https://www.my-website.kr/ (all good)
Access: https://my-website.kr/ (home page again but without www
Result: https://www.my-website.kr/ (all good, redirects my non www
urls to www)
Access: https://www.my-website.kr/subpage
Result: https://www.my-website.kr/subpage (all good)
Access: https://my-website.kr/subpage
Result: https://www.my-website.kr/index.php (not good, I don't want this index.php)
This last test it the one I cannot fix and it is quite annoying. When I access a subpage without www; it's okay if the only solution is to get a redirection to the home page again but at least without the index.php this is terrible for the SEO.
I know these questions about htaccess have been answered many times but I am loosing hope... Even the technical support of my dedicated server couldn't answer me properly.
I have two htaccess files at the moment; one located directly at the root of my public_html/ with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.kr [NC]
RewriteRule ^(.*)$ https://www.domain.co.kr/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^domain\.co\.kr [NC]
RewriteRule ^(.*)$ https://www.domain.co.kr/$1 [L,R=301,NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
And another one under public_html/public with the default Laravel's htaccess content:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
You might ask me why have I two htaccess at two different location. And I wonder the same. The thing is I have done many test and I discovered that putting the www / https rules directly at the root folder kind of worked better. But I might be wrong.
Again sorry if this question has been asked a million time but I couldn't find the answer that would work for me.
Thanks in advance for any bit of help.
The .htaccess file taking care of all the redirects (www or not, https or not) is over-engineered.
It seems like the final domain should always be www.domain.co.kr + SSL so there's no need to use %{HTTP_HOST} as the final domain is not dynamic, or that %{HTTPS}s ^on(s) match to extract s or not.
The order is correct tho: always do all the nitty gritty redirects/http(s) before the framework rewrites.
Because you do that in the parent folder, it's OK. You could put those rules in the same .htaccess file too, but you'd have to put them before the Laravel ones.
I would start with simplifying it:
RewriteEngine On
# Redirect http to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.domain.co.kr/$1 [R=301,L,QSA]
You'll notice I also added the QSA flag to pass any query string you might have in the original request.
See how you go from there.
Also make sure you always empty your browser cache every time you try something new as redirects are cached.
If it's still not working and you can change the log level on your apache config, check this: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging
You'll be able to debug what happens during redirects/rewrites, step by step, to pin point where the actual issue is.

how to redirect domain name with directory

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.

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

Categories