John Davidson

php - htaccess redirect specified requests to subfolder

0 comments
Message:


My current htaccess:


RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ www/index.html [L,QSA]

RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^(.*)$ app/index.php?/$1 [L,QSA]

My problem:


I have several domains pointed to one hosting. Domain domain1.com should be pointed to www directory, domain2.com to app directory. In domain2.com all of request should be pointed to index.php except assets (images, styles, js and so on). Assets are also in app folder (or sub-directories), but as you can see all requests are pointed to index.php. I tried some of solutions from other questions but nothing work.


Example:


domain2.com/some/request/1/2 should be pointed to app/index.php
domain2.com/test.png should be pointed to app/test.png

Can you help me? Thanks a lot for your help.



My Answer:
You can achieve this by adding a condition to exclude requests for assets from being redirected to index.php. Here's an updated version of your htaccess file:

apache
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ www/index.html [L,QSA]

RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|css|js)$
RewriteRule ^(.*)$ app/index.php?/$1 [L,QSA]


In this updated version, the `RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|css|js)$` condition excludes requests for files with extensions png, jpg, gif, css, and js from being redirected to index.php. This allows requests for assets to be served directly without being redirected.

Make sure to adjust the list of file extensions in the condition to match the types of assets you have in your app directory.

Rate this post

4 of 5 based on 2546 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us