views:

956

answers:

4

Hi, I'm developing a php application and I have a little issue with Apache and Mod Rewrite. Anyone knows what's wrong here?:

RewriteEngine on
RewriteBase /mysite/
RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule !^img\/.* index.php

When I put http://localhost/css/css.css appears index.php, maybe I'm missing something... Why when the url matchs with the first rule apache doesn't stop the rewriting process?

'last|L' (last rule)

Stop the rewriting process here and don't apply any more rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('/') to a real one, e.g., '/e/www/'.

I have readed forums and docs since 3 hours and I still have the same problem.

Thanks in advance.

A: 

You might have to set your rewrite base to "/" before starting your expression with "^css..."

RewriteBase /
FlorianH
or just put a / following the ^ char in both the css and js rules.
Cheeso
Ok, my RewriteBase is /mysite/RewriteRule !^img\/.* index.phpworks fine, but the css and js rules doesn't work.Any ideas?
Centauro12
@Cheeso: The leading `/` is only needed if mod\_rewrite is used in the server or virtual host configuration.
Gumbo
+1  A: 

try

RewriteRule ^/css/css\.css css/css.php [L]
RewriteRule ^/js/js\.js js/js.php [L]
RewriteRule ! /^img/.* index.php

ie. if you ^-anchor the pattern to the beginning of the string, start it with a /. patterns are matched against URL-paths, which start with /.

EDIT

above is valid for server config, virtual host, and directory context only. if the context is .htaccess, the per-directory prefix including the first slash is stripped before the rule is matched (and prepended afterwards), so no need for ^/ here.

ax
Sorry, I forgot to say than my RewriteBase is:RewriteBase /mysite/It isn't work for me.Thanks
Centauro12
Most examples over at Apache omit the slash, and I never had problems with matching `^payload` compared to `^/payload`.
Boldewyn
...that is, in a .htaccess context.
Boldewyn
... which he hadn't said when i wrote my answer.
ax
A: 

I've found a solution:

RewriteEngine on
RewriteBase /mysite/

RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^css\/(.*)$ css/$1 [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule ^js\/(.*)$ js/$1 [L]
RewriteRule ^img/(.*)$ img/$1 [L]
RewriteRule ^(.*)$ index.php?rewrite=$1

It works fine, but I don't know why it's necessary

RewriteRule ^css\/(.*)$ css/$1 [L]

and

RewriteRule ^js\/(.*)$ js/$1 [L]

I hope it hepls anyone.

Thanks! :)

Centauro12
+3  A: 

Centauro12, the problem is, that the [L] flag in fact stops propagation through the following rules, but then (if you are in an .htaccess file) the URL mapping starts over again. That means, all your rules will then be processed a second time. See the Apache Rewrite Guide for the details.

Therefore you need to explicitly disable rewriting for your rewritten php scripts:

RewriteRule ^css/css.php - [L]
RewriteRule ^js/js.php - [L]

or more compact (although perhaps not what you want):

# don't rewrite anything that really exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
Boldewyn
Thanks, I didn't know about it.
Centauro12