简体   繁体   中英

Redirect URL with .htaccess to Subdirectory

I am running a website and a CakePHP server on the same service. The CakePHP is in a subdirectory. I would like the URL http://www.example.com to direct to my index.html in the root and http://www.example.com/app to redirect to CakePHP webroot.

These are my current .htaccess files

In var/www/html:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$    app/webroot/  [L]
    RewriteRule  (.*)  app/webroot/$1  [L]
</IfModule>

In var/www/html/app:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ /webroot/ [L]
    RewriteRule (.*) /webroot/$1 [L]
</IfModule>

In var/www/html/app/webroot:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Edit

The question is how would I set up my htaccess so that I can get to my CakePHP webroot with the URL http://www.example.com/app but on the index var/www/html if I use the URL http://www.example.com . With my current htaccess it redirects both URL to the CakePHP webroot.

Thank you

Do you have root access to apache config?

Im guessing your cake app is not the only thing you want to run on this domain so you are wanting to set it up as an alias on /app ?

I recommend setting up /app as an Alias in apache config first.

Im more familiar with doing this on Ubuntu 14.04 (my example is specific to ubuntu) but the concept will be the similar on other Linux distributions

Enable the alias mod:

sudo a2enmod alias

using nano or other favourite text editor you need to add a file to apache config:

sudo touch /etc/apache2/sites-available/mycakeapp.alias.conf
sudo nano /etc/apache2/sites-available/mycakeapp.alias.conf

For the file content:

Alias /app "/var/www/html/app/webroot"
<Directory "var/www/html/app/webroot">
    Options Indexes FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

Enable the config file so apache knows to load it:

cd /etc/apache2/sites-available
sudo a2ensite mycakeapp.alias.conf

Restart apache to load in the new config:

sudo service apache2 restart

then rewrite the base in your .htaccess file in /app/webroot

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Done. http://www.example.com/app will now point to and act as your applications base directory

the other .htacess files /.htaccess and /app/.htaccess can be removed as they will never be hit. Not compulsory to point the virtual host straight to the webroot but it is better practise as it will stop users being able to access or view other folders in the app directory

Centos / RedHat setting up apache alias

Adding this to the bottom of your /etc/httpd/conf/httpd.conf file and restarting apache should do the trick. service httpd restart

Alias /app "/var/www/html/app/webroot" #make sure this is actually your applications root
<Directory "var/www/html/app/webroot">
    Options Indexes FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

The Short Answer to your question

If you dont have root access to apache config or just want to skip the alias adding step you can just add the RewriteBase /app line to your webroots .htaccess

Delete or edit the .htaccess in /var/www/html so that the myexample.com/ doesnt redirect to the cake app

Answer: https://stackoverflow.com/a/33018144/1127933

Your folders structure:

/var/www/html/
  .htaccess <----- edit
  index.html
  app/ <------ cakephp
    bin/
    config/
    src/
    ...

Inside var/www/html add .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^app$    app/    [L]
    RewriteRule    ^app/(.*) app/$1    [L]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM