Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

.htaccess - Yii2: Separate front and backend in advanced-app doesn't work on XAMPP

I simply want to access the backend part via www.domain.comadmin and frontend by www.domain.com. What I did in my root .htaccess is :

Options -Indexes
# follow symbolic links
Options FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/admin/$
RewriteRule ^(admin)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]

RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1

It redirects to the the frontend but doesn't work properly with the backend. Can't realize what is the problem because I am using the same lines of configurations at work and there everything is OK. Can it be because of the XAMPP? We work on Ubuntu and Apache server at my workplace. This is the frontend and backend ( they are similar ) also :

AddDefaultCharset utf-8

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on


# Make the backend accessible via url: http://site/admin
RewriteRule ^admin$ $admin.php [L]

# 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


RewriteRule ^static - [L]

EDIT: I DID IT! What I was set in my httpd-vhost.conf was :

<VirtualHost *:8080>
    ServerName fitness
    DocumentRoot "c:/xampp/htdocs/fitness/frontend/web"
</VirtualHost>

And the problem was that I was redirecting my request to the frontend/web htaccess but what I really needed was the root .htaccess. So I had changed the DocumentRoot to c:/xampp/htdocs/fitness.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I always use the following way when setting up a new project when I am working in the office where we have ubuntu and it always works perfectly.

Follow these steps below, and create 2 virtual hosts with the name www.myapp.com and admin.myapp.com

1) Open terminal and type

  • sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/www.myapp.com.conf.
  • sudo nano /etc/apache2/sites-available/www.myapp.com.conf.

add the following code once the file opens.

<VirtualHost *:80>
       ServerName www.myapp.com
       DocumentRoot "/path/to/project/root/frontend/web"

       <Directory "/path/to/project/root/frontend/web">
           # use mod_rewrite for pretty URL support
           RewriteEngine on
           # If a directory or a file exists, use the request directly
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           # Otherwise forward the request to index.php
           RewriteRule . index.php

           # use index.php as index file
           DirectoryIndex index.php

           # ...other settings...
       </Directory>
</VirtualHost>

2) Close & save the file and then in terminal write

  • sudo a2ensite www.myapp.com.conf
  • open hosts file sudo nano /etc/hosts and add in the end on a new line 127.0.0.1 www.myapp.com.
  • Restart apachesudo service apache2 restart

3) Repeat the above steps for the backend and change the file names and ServerName, Directory and DirectoryRoot respectively.

4) Just make sure you have a .htaccess file in the frontend/web and backend/web with the following

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

And thats it you wont need any other .htaccess file other than these now type in the browser www.myapp.com or admin.myapp.com and see it working.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...