htaccess rewrite rules for multiple domains (symfony 2 in subfolders) - .htaccess

straight to the point.
I have a web hosting with 2 domains pointing to the root directory of the hosting.
I have 2 seperate projects in the root directory both symfony2
I need to redirect the domains to the seperate directories using .htaccess.
My current .htaccess file in the root folder
RewriteEngine On
#www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301, L]
#domain1
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain1/web
RewriteRule ^(.*) /domain1/web$1 [L]
#domain2
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain2/web
RewriteRule ^(.*)$ /domain2/web$1 [L]
now when I type in domain1 in the adress bar. The project works correctly. What I get in the adress bar is: "domain1.com/domain1/web" And I'd like to get rid of the domain1/web part so that the functionality stays. Do I somehow use rewrite base? Or is there an other way for me to get my result? please help. Note that I'm not really used to working with .htaccess so please be gentle :)

Related

Change Root Folder Directory and Redirect to HTTPS with .htaccess

I don't know much about the .htaccess file and need some assistance please. I have a few websites hosted one one CPanel account. I didn't want to have my "main" site stored directly in the public_html folder, so I moved it into a subfolder and then found this .htaccess configuration that allows me to load my main site from that subdirectory. So that is working.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/example_directory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /example.com/$1
# Change example.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 site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.html
So as I said, the above .htaccess will allow me to store my site files in a different folder rather than right at the root. But now I'm trying to force a redirect for this site over https as well. I found this site here:
Force Website to Use SSL
But, since I really don't understand the .htaccess file I'm having trouble integrating these two features together. Any help on this would be greatly appreciated. Thanks.
I'm pretty certain I figured this out myself. I just simply had to add this to the end of my .htaccess file:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
and it works like a charm. Full .htaccess file looks like this, which changes the folder directory for the site to use and then forces it over https.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/example_directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.html
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}

Laravel 5 shared host subdomain htaccess

Firstly because im on a shared host I cannot place contents of my public folder directly into root. I have to place the entire project into the public_html directory and use a .htaccess to point to the public folder like below (which works fine for a single Laravel project):
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
I now created a subdomain with the host and it created a directory inside my public_html directory called store.domain.com. This will be for an entirely different Laravel project. If I place the regular folder structure with the .htaccess file into this subdomain folder I get a 503 error when accessing the subdomain. I spoke to the host and when they suggested removing the root .htaccess file the subdomain started working but the main domain fell over.
Ive been struggling for days now reading up various solutions to other peoples similar issues on WordPress but cannot get anything to work correctly. The solution below sort-of works but it keeps adding on the public folder to my URL for the subdomain (root domain project is working fine):
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^store.mc.com
RewriteRule ^(.*)$ /public/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^store.mc.com$
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ /public/$1 [L]
Anyone had this issue or know how to solve it?
I dont really understand why this works but the below fixed my issue:
RewriteBase /
RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
I uploaded my website files to a subdomain, and I always see this error
You don't have permission to access this resource. Additionally, a 403
Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
Just create a file .htaccess In the subdomain folder
Add this to it and save .
RewriteBase /
RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
Your RewriteCond is invalid without the leading slash.
change :
RewriteCond %{REQUEST_URI} !^public
to
RewriteCond %{REQUEST_URI} !^/public

Redirecting files from a directory to subdirectory causes infinite loop

Looked on here at all the answers regarding this issue but none fixes my issue. I have a directory called pdfs which used to contain all my .pdf files. They are now all inside pdfs/sales so trying to create a redirect for all files in the pdfs directory to look inside the pdfs/sales directory instead using a .htaccess file in the root of my site. Everything I've tried so far results in an infinite loop. Here's what my .htaccess looks like so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first rule redirects all www. traffic to non-www. url. The second rule is my pdfs rule. The last rule is for redirecting all requests to index.php for seo friendly urls. Is there a conflict here? Where am I going wrong?
Thanks
You can keep your rule like this (my comment inline):
DirectoryIndex index.php
RewriteEngine on
# remove www from domain name
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
# for all non-files and no-directories route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Make sure to test it after clearing your browser cache.

.htaccess rewrite/redirect directory to subdomain

In my website's root .htaccess file, I have the code shown here to redirect to the www version, as well as hide the index.php page if in the directory root:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteRule ^index\.php$ http://www.example.com/ [R=301,NC,L]
Within this site, I have an API located at http://www.example.com/app/API/
I would like to do two things:
Perform all API calls via the subdomain http://api.example.com/ (but really process them at the original URL, http://www.example.com/app/API/)
Redirect all traffic from http://www.example.com/app/API/ to http://api.example.com/, keeping all query parameters in tact.
What I'm asking seems fairly simple, but for whatever reason I can't figure it out (even after extensive searching). I can set up the subdomain through my hosting provider, but I'd rather use .htaccess for further customization.
Try:
RewriteCond %{HTTP_HOST} !^api\. [NC]
RewriteRule ^app/API/(.*)$ http://api.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^api\.example\.com$ [NC]
RewriteRule ^(.*)$ /app/API/$1 [L]

htaccess - hosting multiple domains on one shared server,

I have a site1.com as my primary site. I created two folders in the root directory "site1" & "site2". I used Htaccess to redirect site1.com's traffic to folder "site1". Site2 works too as site2.com using directory "site2".
Now, the issue is because site1 is primary, I'm able to visit site2 via site.com/site2 . What rewrite can I use to ignore site.com/site2, or show a 404 when site1.com/site2 is visited and still allow for site2.com to work?
Here is my htaccess file.
RewriteEngine on
Options -MultiViews
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
My folder hierarchy is
-root
.htaccess
-site1 folder
- index.php
-site2 folder
- index.php
Due to godaddy's primary domain setup, even though when i visit site.com it loads info from site1 folder, if i visit site1.com/site2 it will load site 2
I went in a different direction for a similar problem on Godaddy.
Rather than looking for just hits from site1.com and redirect them, I sent any requests hitting the site2 subfolder / subdomain to site2.com with this:
RewriteCond %{HTTP_HOST} !^site2\.com$ [NC]
RewriteRule ^(.*)$ http://site2.com/$1 [R=301,L]
Now any requests that worm their way into that site1.com/site2 subfolder get 301'd to the appropriate page on site2.
I don't consider myself an expert by a longshot and would welcome critiques from those with more experience.
Untested code: I believe you can do the following
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^site2/(.*)$ http://site1.com/$1 [R=301]
This will (or should) redirect the client from http://site1.com/site2/my/awesome/page.php to http://site2.com/my/awesome/page.php.

Resources