Need to organize several sites in one hosting account, putting each into its own folder.
Via CPANEL, this is completed and working for the add-on domains, but the main site (with all its subfolders) also needs to be in its own folder.
I have been doing this via PHP:
example.com/index.php :
<?php
header('Location: subdir/mainsite/index.php');
?>
The down side is that there is a page flash as the site redirects.
Another downside is that the URL reflects the new location in the address bar:
http://example.com/subdir/mainsite/
I just want visitors to see http://example.com
I suspect this can be resolved by using htaccess instead of PHP.
So, if my domain is:
http://example.com
And I place the main site files in:
public_html/_sub/mainsite
And the add-on sites must continue to work...
What would the htaccess look like?
This is what I have thus far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/_sub/mainsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_sub/mainsite/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ _sub/mainsite/index.php [L]
Related
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]
At first glanse question is pretty common, however other answers I have seen do not work in my case.
I need to redirect a page from one domain to another domain. Both domains have one web-site under them and one common .htaccess file.
www.olddomain.com/guides -> www.newdomain.com
I use .htaccess file. If someone knows answer for other way - you are welcome also.
My current file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
I have tried this way:
RewriteEngine on
Redirect 301 http://www.olddomain.com/guides https://www.newdomain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Also I tried this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://www.olddomain.com/guides$ https://www.newdomain.com [R=301]
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Does not work. How it can be done properly?
I believe your Redirect 301 line is the way to go, but should read something like this:
Redirect 301 /guides https://www.newdomain.com
See here for more information.
Edit to add:
The above will redirect to https://www.newdomain.com/
If you want a certain path, then just put
Redirect 301 /guides https://www.newdomain.com/foo
where foo is the path on the server you want to end up at.
I end up with the following.
Unlinked "olddomain" name from the origin site.
Created new website folder at my hosting with only one file .htaccess in a root that contains only one line as #CBaish suggested:
Redirect 301 /guides https://www.newdomain.com
Linked "olddomain" to this folder
Now all traffic from the page "/guides" of olddomain will go to my newdomain.
I am trying to point the main domain from my host (https://website.com) which right now just goes to public_html to a subfolder in the public_html directory (new.website.com) with the user still seeing https://website.com without changing.
I tried doing what is found in here: https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
And added this code to the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteCond %{REQUEST_URI} !^/new.website.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new.website.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteRule ^(/)?$ new.website.com/index.php [L]
However 2 things happened incorrectly:
The domain changed to https://new.website.com/404.html
It is giving me a page to proceed with caution since new.website.com does not have a certificate.
(folder name and also a subdomain are both named: new.website.com)
How can i redirect so it still shows https://website.com and go to the wordpress folder?
If I understand your question right, you need to have website.com visible to the public but actually, WordPress is installed on new.website.com
In this case, you need to do Wordpress multisite.
Check: https://codex.wordpress.org/Create_A_Network
I am trying to do something for 2 website. The main one is on www.personal.com then there is a wordpress blog hosted on another server could be loaded on blog.personal.com
I want to mask the urls as main site with /blog/ architecture and load (with transparency) the same sub domain urls
Example : www.personal.com/blog/my-first-article will load blog.personal.com/my-first-article BUT with keeping this url in the browser (the url in browser will ne change) :
www.personal.com/blog/my-first-article
Last difficulty, the .htaccess file is used by others websites I host, then I do not want theses rules for others domain than personal.com The rule should be only for one domain name.
Does anyone know how to to this ?
Solved, the right thing was, in the Apache VHOST configuration :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.personal\.com$
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^blog/(.*)$ http://blog.personal.com/$1 [P,QSA]
ProxyPass /blog/ http://blog.personal.com/
RewriteCond %{HTTP_REFERER} ^http://www\.personal\.com/blog/ [NC]
RewriteCond %{REQUEST_URI} ^(/[^/]+/)
RewriteCond %{REQUEST_URI} \.(jpg|png|css|js)$
RewriteRule ^(.*)$ http://blog.personal.com/$1 [L,QSA,R=301]
I have a domain let's call it newsite.com which is parked to maindomain.com. I use mod-rewrite to direct all requests for newsite.com to a subdirectory on maindomain.com. On newsite.com I have CodeIgniter installed. Which means the urls look like this:
newsite.com/products/shoes/
The above works just fine.
However, if I set up a controller for a page like this:
newsite.com/about/ or newsite.com/about/faqs/
I get a 404 which I believe is being caused because it is looking for the page maindomain.com/about.php which does exist, but does NOT exist on newsite.com.
My question is.... how do I PREVENT urls like newsite.com/about/ from pointing to newsite.com/about.php ? This is the opposite of what many people try to do (they tend to want to allow the file extension to be missing).
I'm wondering if it is and apache or PHP setting that causes it to look for the file first if it exists and the directory does not? Or do I need to add more mod-rewrite rules?
Thanks very much!
Edit - here is my .htaccess file currently. It resides at the web root of the maindomain.com shared hosting account.
# Redirect various urls to subfolders of the format: /site-thename/
RewriteEngine On
# Remove the www for NewSite.
RewriteCond %{HTTP_HOST} ^www.newsite.com$ [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
# Handle pages and directories that should not go through CodeIgniter.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
RewriteCond %{REQUEST_URI} ^/demo/ [OR]
RewriteCond %{REQUEST_URI} ^/robots\.txt [OR]
RewriteCond %{REQUEST_URI} ^/emailform\.php [OR]
RewriteCond %{REQUEST_URI} ^/assets/ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php
Rewriterule ^(.*)$ /site-newsite/$1 [L]
# Handle CodeIgniter URLs.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
Rewriterule ^(.*)$ /site-newsite/index.php?/$1 [L]
Asked my hosting company's support department what might be causing this... and it turns out that it is caused by Apache MultiViews. This can be turned off via
Options -MultiViews
in the htaccess file. That worked.
Thanks for the suggestions.
If you have no rewrite rules for it, Apache, by default, will reference whatever is on the filesystem. You must have rewrite rules there to tell Apache to use a specific controller to handle requests instead of attempting to reference objects on the filesystem.
I assume that if you are an apache user, you use .htaccess file for redirecting the URL to you 'index' file (let's call it index.php). Also you should have sth like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
in your .htaccess file. Note that the RewriteCond condition has a parameter which checks if it is not a file:-) More can be found at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond