3

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

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.

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

12 Comments

Works with a VirtualHost, but if I try that in a normal url (without a host): http://localhost/test/index.php doesn't work.
How can I configure that for work in a sub-folder without a host/virtualhost? Like http://localhost/test/index.php?
@PatrickMaciel Just put the htaccess file in the folder that your page is in (e.g. /test/)
I do that but doesn't work: GET http://localhost/css/styles.css 404 (Not Found)
@PatrickMaciel if you want to rewrite http://localhost/css/styles.css, then it must go in the document root, since /css/ is off of the root directory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.