How to set-up different URLs for yii frontend and backend - yii2

For example, if I want localhost/basic.com for the front-end and localhost/basic/admin for the back-end.
I am new to the framework, however, it seems really interesting.
Please help.
I have watched tutorials and all but none seem to cover this.

You can use the following .htaccess
Options FollowSymLinks
AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
RewriteEngine On
# the main rewrite rule for the frontend application
RewriteCond %{REQUEST_URI} !^/(backend/web|admin)
RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L]
# redirect to the page without a trailing slash (uncomment if necessary)
#RewriteCond %{REQUEST_URI} ^/admin/$
#RewriteRule ^(admin)/ /$1 [L,R=301]
# the main rewrite rule for the backend application
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(.*) /backend/web/$1 [L]
# if a directory or a file of the frontend application exists, use the request directly
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . /frontend/web/index.php [L]
# if a directory or a file of the backend application exists, use the request directly
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . /backend/web/index.php [L]
RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git)
RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
</IfModule>
FUll configurations can be found at: https://github.com/mickgeek/yii2-advanced-one-domain-config

Related

How To make Custom Links Without Showing page not found error [duplicate]

I want to redirect all pages on my site (including index) to UnderWork.html
and I'm doing this using .htaccess with this code:
RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html
...and its works fine.
Now I am adding some more code to my .htaccess to redirect all traffic to the non-www domain and now my code looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html
Here is where I have a problem. If I enter a URL like: domain.in/xyz then it works as before, but if I use a URL like: www.domain.in/xyz then Apache converts it to coincart.inxyz/.
What am I doing wrong here? How can I get what i want? Thanks in advance.
Below are rules that work for me:
RewriteEngine On
# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]
# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]
# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]
To show a custom page when a directory root (domain root in particular) is requested, use the Apache's DirectoryIndex directive:
DirectoryIndex UnderWork.html

Apache Mod Rewrite for CodeIgniter AND HTML5 mode

I have Codeigniter running on Apache and have Apache Mod Rewrite that works well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've now changed the front-end to be HTML5 and my app uses emulated URLs that suppose to be handled by the same 1-page application.
So I need something like the following URL rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
So, I'd like to combine those together.
First I'd like to see if the first part of URL path is a CI controller (a file in ./application/controllers) and if yes then rewrite it to index.php.
If not, I'd like to rewrite it to index.html.
I'm trying to build something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# -- RewriteCond ^/([^/]+)/ (application/controllers/$1.php) is a file
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^ index.html [L]
I understand that my other option it to manually specify all possible routes and controllers but would be nice to do it automatically...
UPDATE:
The proposed solution below helped me to solve the problem.
This is what worked for me eventually:
RewriteCond C:/projects/someproject/WS/application/controllers/$1.php -f [OR]
RewriteCond C:/projects/someproject/WS/application/controllers/$1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/$
RewriteCond C:/projects/someproject/WS/application/controllers/%1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/[^/]*/?$
RewriteCond C:/projects/someproject/WS/application/controllers/%1.php -f
RewriteRule ^(.+)$ index.php/$1 [L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html [L]
I've realised I had to check for more types of URLs and also I found I couldn't use %{DOCUMENT_ROOT} because I'm using VirtualDocumentRoot settings and DOCUMENT_ROOT isn't working
Try :
# If /document_root/application/controllers/request.php is a file
RewriteCond %{DOCUMENT_ROOT}/application/controllers/$1.php -f
# Rewrite the request to /index.php
RewriteRule ^(.+)$ /index.php/$1 [NC,L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.html

htacess: triple rewrite HTTPS + no-WWW + no-.html

I am trying to write a multi rewrite function in htacess.
change http into httpS
remove www
remove .html
It should handle all the cases:
http://www.example.com/about.html
should be rewritten as
http S://example.com/about
But of course if there is only one item to rewrite it should work too:
http://example.com/about
should be rewritten as
http S://example.com/about
Here is my code (https and no-WWW work but the no-.html which is commented does not work).
Please note that when rewriting you need to use the previously rewrited URL and not what the user typed into his address bar otherwise the rule will overwrite the change that another rule did.
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
#remove .html if any
#RewriteCond %{REQUEST_FILENAME}.html -f
#RewriteRule ^(([^/]*/)*[^/.]+)$ /$1.html [L]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off
You can use:
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
#remove .html if any
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off

Point image request to a higher folder thru htaccess

Is there any rule I can write to make all requests to my image folder point up one folder. I'm dealing with a lot of links so I don't want to put ../
Eg: http://hostingxtreme.com/ajax/images/logo.png.
Should become http://hostingxtreme.com/images/logo.png
Thanks
Here the code I'm using
Dosent seem to be working!
php_value register_long_arrays On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
</IfModule>
In the htaccess file in your document root, add either:
Redirect 301 /ajax/images/logo.png /images/logo.png
Or if you need to use mod_rewrite:
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
I got it!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)images/(.*)
RewriteRule (ajax/|ajax/.*/)images/(.*) images/$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)stylesheets/(.*)
RewriteRule (ajax/|ajax/.*/)stylesheets/(.*) stylesheets/$2

HTML5 Boilerplate .htaccess rewrite to index.php

My question is:
if i add the following code at the bottom of .htaccess file of HTML5 Bilerplate
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</IfModule>
this will send all REQUEST_URI to my index.php so I can handle them but will it break some of the rules from the .htaccess file already defined above? And if its wrong, what should be added?
The rules you have posted above are already present in the .htaccess of HTML5 boilerplate (under the heading # Built-in filename-based cache busting)
# Uncomment to enable.
# <IfModule mod_rewrite.c>
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
# </IfModule>
You can uncomment them by removing the '#' at the beginning of each line.
And change the 5th line to allow your rule for favicon.ico:
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif|ico)$ $1.$3 [L]
Edit: To direct all requests to index.php, I have modified the rules under # Suppress or force the "www." at the beginning of URLs in this way:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(js|css|png|jpg|gif|ico)$
RewriteRule ^(.*)$ index.php [NC]
</IfModule>
In general you should put your own rules at the beginning of the .htaccess file before the other rules, not after those.