.htaccess - force to attach https, force to fall into a subfolder, force www, do not break the site

I find it very difficult to edit the .htaccess file to do these three things together. I was able to get each part separately, but I just don’t understand how the logical thread works to make them work.

This is the best I managed to put together with a demo with the support of bluehost http://helpdesk.bluehost.com/index.php/kb/article/000347

I appreciate any help in solving this problem.

# Use PHP5 Single php.ini as default AddHandler application/x-httpd-php5s .php # Bluehost.com # .htaccess main domain to subdirectory redirect # Copy and paste the following code into the .htaccess file # in the public_html folder of your hosting account # make the changes to the file according to the instructions. # Do not change this line. RewriteEngine on # Change yourdomain.com to be your main domain. RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$ # Change 'subdirectory' to be the directory you will use for your main domain. RewriteCond %{REQUEST_URI} !^/folder/ # Don't change this line. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Change 'subdirectory' to be the directory you will use for your main domain. RewriteRule ^(.*)$ /folder/$1 # Change yourdomain.com to be your main domain again. # Change 'subdirectory' to be the directory you will use for your main domain # followed by / then the main file for your folder, index.php, index.html, etc. RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$ RewriteRule ^(/)?$ folder/index.php RewriteCond %{HTTP_HOST} ^sampleurl\.com$ [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{SERVER_PORT} 80 RewriteRule (.*) https://www.sampleurl.com/$1 [L] # For security reasons, Option all cannot be overridden. # Options All -Indexes # Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes 

Update:

Well, after a long time, using the following, I ran into problems.

 IndexIgnore * RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} !^/site/ RewriteRule (.*) https://www.example.com/site/$1 [L,R] RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} ^/site/ RewriteRule (.*) https://www.example.com/$1 [L,R] RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 443 RewriteCond %{REQUEST_URI} !^/site/ RewriteRule (.*) https://www.example.com/site/$1 [L,R] 

New problems.

  • Bluehost has an app that quickly installs and updates open source apps like wordpress, joomla, phpbb, etc. Apparently, my new super.htaccess file completely destroys their system. The only way I can run updates and installations is to move .htaccess to .htaccess.bak before the update or installation is complete. Then move it back.

  • I cannot create any .htaccess on other sites that I host in other directories. for example I have a main account that I store in / www / site / that this script requests requests. Unfortunately, my other sites stored in / www / site2 / and / www / site3 / do not work well. First I need to create an empty .htaccess so that it cannot pull the .htaccess above found in / www /. For some reason, if I try to do something like the power of www.site2.com, I get 500 errors stating that I force too many redirects.

I have a theory that my solution above should be a little more specific to make sure that it only triggers a request sent to example.com. (I think the line with anything not containing / site / is a bit wider).

I appreciate any support allowing this. Save me a little headaches!

Update II:

I do not believe that I have access to apache configuration. Only the parameter that I see in cpanel is apache handlers (not quite sure what this section does).

I will send a ticket to find out where the script server is running from. I believe that it works from "https://www.simplescripts.com". What changes would you recommend, I will add to ignore this domain?

My bluehost host launches you with the main account and then subdomains. The primary location of the account is specified by \ www. Unfortunately, this gets messy when you try to host multiple domains. That's why I use .htaccess to force access to this directory in the \ site \ directory. Thus, capturing the primary site in the \ site \ directory.

I think the transfer problem that I had with one of my subdomains was caused by WordPress trying to force a domain without www, and then I write a .htaccess file that forces www. This caused an endless loop, not my .htaccess file in question. If I can solve the problem with the templates, then I gmoney;)

+6
.htaccess mod-rewrite
source share
2 answers

I think something like this will work

 RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} !^/folder/ RewriteRule (.*) https://www.example.com/folder/$1 [L,R] RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} ^/folder/ RewriteRule (.*) https://www.example.com/$1 [L,R] 

but I admit that I did not check. You can add everything without redirecting if the file exists etc. if you want.

If you have not seen this, here is the mod_rewrite documentation .

change

In response to an updated question:

The first thing I noticed is that it seems that you can have enough control over your installation to put rewritable files in Apache configuration files (in VirtualHost sections) and not in htaccess files, which will solve your problem right away your other sites.

First problem: it looks like you need to add a RewriteCond to exclude those URLs that the Bluehost program uses. Either that, or access the Bluehost software under another VirtualHost (after you put the rewriting material inside VirtualHost blocks).

If you cannot edit the Apache configuration, then the easiest fix for other sites is not to make them a subdirectory (in the file system) of your main site. In any case, this is rather strange. Move your main site to the /www subdirectory, if necessary.

Personally, I put all the sites in /srv/www/com.site-name.www on my servers (i.e. in the reverse order, first with the top-level domain), it groups related groups of elements when sorted alphabetically. For example, when you have www.example.com and static.example.com)

+4
source

This seems to do everything I need. Probably a shorter way to do this, but it works! IndexIgnore * RewriteEngine On

 RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} !^/folder/ RewriteRule (.*) https://www.example.com/folder/$1 [L,R] RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} ^/folder/ RewriteRule (.*) https://www.example.com/$1 [L,R] RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{SERVER_PORT} 443 RewriteCond %{REQUEST_URI} !^/folder/ RewriteRule (.*) https://www.example.com/folder/$1 [L,R] 
0
source

All Articles