Redirect css (assets) with htaccess

.htaccess, apache, php, redirect

Solution

You don't want that leading slash in your rule's pattern. `RewriteRule`'s that are in the htaccess file will have the leading slash stripped off before applying the rule:

RewriteRule ^css/(.*)$ /app/themes/default/css/$1 [NC,L]

Problem

Problem I want to redirect my css files with htaccess, because the entire path is long. I try this - `index.php`: ``` <link rel="stylesheet" href="/css/styles.css"> ``` And this in my `.htaccess` ``` RewriteEngine on RewriteRule ^css/(.*)$ /app/view/templates/default/frontend/css/$1 [NC,L] ``` But, the CSS was not loaded. ``` GET http://localhost/css/styles.css 404 (Not Found) ``` Environment: development - Without a virtualhost - Direct Access: `http://localhost/test` Folder Structure Apache Directory (Document Root): ``` /home/patrick/workspace/ = http://localhost ``` My "problem" (test) folder ``` /home/patrick/workspace/test/ = http://localhost/test ``` Inside /test folder CSS path (inside test folder) ``` /app/view/templates/default/frontend/css/ ``` So, I want: ``` <link rel="stylesheet" href="/css/styles.css"> ``` And then, redirect (call) the correct url, like that: ``` /app/themes/default/css/styles.css ``` I try that but doesn't work: ``` RewriteEngine on RewriteRule ^/css/(.*)$ /app/themes/default/css/$1 [NC,L] ``` Observations 1) I do not want create a `VirtualHost/Alias` for that URL. My problem is about: - .htaccess - Sub-folders - RewriteRule 2) I can't put a .htaccess in my DocumentRoot directory of apache Why? Because is my development machine and I have a lot folders inside root. For example: - blog - projects - portfolio - study - test - work All items above is a folder in my `workspace`, and each folder is about different things, so, I don't want to put `.htaccess` in root, UNLESS it really needed.

Original source