简体   繁体   中英

Rewrite root to subfolder (.htaccess) except some directories

I have following folder structure:

  • app/ (contains index.php, js/, public/)
  • v1/
  • .htaccess

Requests to v1 should work as usual All other requests should go to /app without changing the url

www.xxx.com/           (displays: app/index.php)
www.xxx.com/v1         (displays: v1/index.php)
www.xxx.com/js/xxx.js  (displays: app/js/xxx.js)

Is this possible?

Basically, static folders: v1, app/js (without app), app/public (without app)

All other requests go to app/index.php without url changes.

To rewrite requests from /root to /subfoldwr you can use the following Rule in your /.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /app/index.php  [L]

This will rewrite everything except existing files/directories from your /root fodder to the /app/index.php .

You may use this rule in site root .htaccess:

RewriteEngine On

# landing page
RewriteRule ^/?$ app/index.php [L]

# if v1/file exists then use it
RewriteCond %{DOCUMENT_ROOT}/v1/$1 -f
RewriteRule .+ v1/$0 [L]

# if app/public/file exists then use it
RewriteCond %{DOCUMENT_ROOT}/app/public/$1 -f
RewriteRule .+ app/public/$0 [L]

# if app/js/file exists then use it
RewriteCond %{DOCUMENT_ROOT}/app/js/$1 -f
RewriteRule .+ app/js/$0 [L]

# if not a file, not a dir and not starting with v1    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^v1/ app/index.php [L,NC]

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