Search code examples
.htaccesscakephpurl-rewritingprefix

Use domain as "prefix" on CakePHP


Let's say that I have a site running CakePHP and I have the prefix "product".

I have lots of pages with URL like:

http://mysite.com/produt/blue-shirt/details
http://mysite.com/produt/blue-shirt/order
http://mysite.com/produt/skate/details
http://mysite.com/produt/sun-glasses/vendors

Now I need to use a domain like http://mysite-blue-shirt.com/ as "shortcut" to the blue-shirt product, and my URLs will become:

http://mysite-blue-shirt.com/details
http://mysite-blue-shirt.com/order

What I need to do?

I think that's something with the .htaccess on the root of the website, outside the app directory.

Here's the current syntax:

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

Solution

  • Try this:

    <IfModule mod_rewrite.c>
       RewriteEngine on
    
       RewriteCond    %{HTTP_HOST} ^mysite-blue-shirt\.com
       RewriteCond    %{REQUEST_URI} !^/product/blue-shirt
       RewriteRule    ^(.*)$ app/webroot/product/blue-shirt/$1 [L]
    
       RewriteRule    ^$ app/webroot/    [L]
       RewriteRule    (.*) app/webroot/$1 [L]
    </IfModule>
    

    The first line checks the site's name. The second one checks if it has already not been re-written to blue-shirt. The final line carries out the rewrite.