How to configure .htaccess for using Yii2 with Opencart in subfolder - apache

I have an issue with proper configuration .htaccess file for Yii2 basic application.
I want to use Yii2 framework with Opencart store which a have installed to the /shop subfolder.
According to Yii2 server setup guide, I need to set /web directory as a DocumentRoot. But then my /shop directory (which is located in a root of Yii2 installation) becomes non web-accessible.
So I decided to break the rules and set my Yii-root as DocumentRoot and redirect all of the requests to the /web dir with assistance of Apache's mod_rewrite.
Please help me to create valid .htaccess file for Yii-root folder (in Opencart folder I've put it's default .htaccess file with just changing RewriteBase to /shop. It seems work properly)
I need something like this:
Lets assume I set DocumentRoot /var/www, then:
http://sitename.com => /var/www/web/index.php
http://sitename.com/any-non-file-request => /var/www/web/index.php
http://sitename.com/favicon.ico => /var/www/web/favicon.ico
http://sitename.com/shop => /var/www/shop

Works now. .htaccess file in /var/www (root of Yii2 installation)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/web
RewriteRule ^(.*)$ web/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^web/(.*)$ web/index.php [L]

Related

General .htaccess rewrite rules for multiple server directory configurations

I have the following directory structure on my local development web server:
/www
/f3-app-1
/application
/config
/core
/resources
/ui
/index.php
/f3-app-2
...
/f3-app-3
...
I keep all of my web apps (f3-app-1, f3-app-2, ...) in sub-folders of the document root. I am accessing the app with http://localhost/f3-app-1/ and I am not using any virtual hosts.
I need to redirect everything (except for files in ui and resources) to index.php. To achieve this I am using something like the following in .htaccess file:
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]
RewriteCond %{REQUEST_URI} !^/f3-app-1/ui/
RewriteCond %{REQUEST_URI} !^/f3-app-1/resources/
RewriteRule .* index.php [L,QSA]
My hosting provider's production server is using virtual hosts to allow multiple websites. After transferring files to their web server I need to change the .htaccess file to something like:
...
RewriteCond %{REQUEST_URI} !^/ui/
RewriteCond %{REQUEST_URI} !^/resources/
RewriteRule .* index.php [L,QSA]
Is there any way to make these mod_rewrite rules to work relative to web application's or .htaccess directory?
You can make /f3-app-1 optional:
RewriteCond %{REQUEST_URI} !^(/f3-app-1)*/ui/
RewriteCond %{REQUEST_URI} !^(/f3-app-1)*/resources/
But this is bad practice.
The proper way to do it is to have virtual hosts on your development environment too. This will solve this, and any other issue related to paths in general.

Writing an .htaccess to point to a public directory in a subdomain of my shared host

I've created a subdomain on my shared hosting to run an Zend Framework 2 application. I developed the application at home where it sits within a virtual host then uploaded it to the subdomain's root directory. The root directory can be found at:
public_html/mysubdomain/
I want to write an .htaccess file that can direct all requests to the public directory with mod_rewrite. My .htaccess is as follows
<VirtualHost mysubdomain.mydomain.com:80>
ServerName mysubdomain.mydomain.com
DocumentRoot /home/myusername/public_html/mysubdomainfolder/public
RewriteEngine off
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</Location>
</VirtualHost>
.. but I get a server error (500). Basically when the user enters mysubdomain.mydomain.com/users/login into the browser, it will re write the URL to the public directory and the index.php file will handle things from there.
Can anyone offer any advice on what I've done wrong here? (or how I should be doing it) I took this from the ZF2 htaccess page and modified it to my domain. I can't see what's missing from it (although obviously something :)
Thanks

Pointing subdomains to a "directory"

I have two questions regarding pointing subdomains to a directory:
Currently I run local, but I can run my site on a fake domain i have set up (with hosts file), its called mysite.com. How can i (by server settings?) do so All subdomains will point to / ? Example anders.mysite.com should show mysite.com and asdassdd.mysite.com also.
Maybe 1. is not necessary, but how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
Why i thought of 1. is because I do not want to specify anywhere in the htaccess or any apache/domain setting, what the subdomain are, since this will be dynamic (created by logged in users in my webapplication)
Currently the users can use mysite.com/anders/ which is a URI they have created, and not a real directory. In the Kohana bootstrap I am then grabbing the URI and showing the relevant user page.
I am using Kohana MVC framework and have this in my htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Any help appreciated!
For 1, in your vhost just add a ServerAlias:
ServerAlias *.mysite.com
Then any subdomain (www included) will get pointed to the same document root.
If you want to put subdomains in their own directory (and since you have access to server config), you can use mod_vhost_alias and the VirtualDocumentRoot directive:
VirtualDocumentRoot /path/to/your/htdocs/%1
So if you request blah.mysite.com, you'll end up in /path/to/your/htdocs/blah as the doc root.
If it's a matter of you needing directory names of non-existent directories in the URI so that Kohana can route them, you'll need to make sure you have mod_proxy loaded:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,P]
To proxy the request back to Kohana with the right URI.
how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
You can use this rule:
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ /%1%{REQUEST_URI} [L]

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

.htaccess, Laravel & Ikonboard

I have to admit, creating mod-rewrite rules still confuses me! So, I'm after some help please...
I've taken on a site built in Laravel, but now need to add an existing forum into the domain. The forum is Ikonboard, which on the live site lives in the cgi_bin folder. When I copy this to the new site, I can't get access because the htaccess is rewriting ALL URL's to the public folder (where Laravel want's it).
So, how can I make any requests to the cgi_bin folder work as well as keeping the rewrite to public for Laravel?
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
Thanks
Simple:
RewriteCond {%REQUEST_URI} !^/cgi_bin
Add this above your other REQUEST_URI rule. If cgi_bin is in the URI, then it will stop rewriting.
Edit - Based on your response with regards to the directory structure, it seems to be wrong. Your www directory is your public directory. So, your structure should look like this:
/www/ (your public folder)
bundles/ (etc...)
cgi_bin/
.htaccess
index.php
/laravel/
(etc...)
Change your directory structure to look like that, and make sure that your .htaccess file has the following in it:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you call a CGI document, it should pass through because %{REQUEST_FILENAME} !-f is set above. All assets should pass through as well. You do not need to specify whether or not the cgi_bin folder is being requested. If it does not work, then add the line as I had originally in this answer (except, you would put the rule just under RewriteEngine On.