Htaccess rewrite rule from repertory load sub domain urls - php

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]

Related

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/

Redirect root domain with https to subfolder with wordpress

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

Move main site into subfolder (htaccess)

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]

force https for few domains ot of all using htaccess but all domains shared same files

I have setup system so all different domains used the same files and separate content or product based on the store id assigned to each domain.
Now I want to force only 2 domains to use https for all pages using httaccess.
Say, I have 5 domains abc.com, qwe.com, xyz.com, rty.com & uio.com
now I want abc.com & xyz.com to force https, not for other domains.
How I will rewrite rules for this in htaccess file?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(abc|xyz)\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R]
in the htaccess in your document root.
This should work for your case:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.com
RewriteCond %{HTTP_HOST} ^xyz\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}.com$1 [R=permanent,L]

Prevent url with a directory from pointing to a web page

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

Categories