4

I've got a problem with rewriting urls. The following is happening:

http://www.example.com/scores
http://www.example.com/registreren
http://www.example.com/login

these urls will be redirected to index.php?route=scores etc

This is all working very well. But now I've got template files in a subdirectory like images and stylesheet. These files are in

  • template/css/style.css
  • images/images.png etc

now all those files are also being redirected to index.php?route. I'm aware of the leading slash and all links to the files are absolute paths.

The following code is being used in the .htaccess file:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?route=$1 [L,QSA]
2
  • For testing comment out your RewriteRule above and then try to click: your css
    – anubhava
    Apr 11, 2012 at 13:14
  • The links to "your css" in comments below (and above) - that you say are being "redirected" - are missing the /css path segment as stated in the question?
    – MrWhite
    Jan 17, 2020 at 2:50

2 Answers 2

1

Try to add a RewriteBase with this code:

RewriteBase /

Depending on the server configuration it is possible that %{REQUEST_FILENAME} contains a slash at the begining which would produce an absolute and not a relative path.

4
  • I tried to add the RewriteBase, Also doesn't work. The stylesheet is just beeïng redirected link Apr 11, 2012 at 10:37
  • Try to pass as second parameter %{REQUEST_FILENAME} and echo the value.
    – rekire
    Apr 11, 2012 at 10:42
  • If possible you could also try to set the mod rewrite loglevel higher and post your log file.
    – rekire
    Apr 11, 2012 at 18:07
  • The RewriteBase directive would only be required here if the .htaccess file was not in the document root or the RewriteBase directive had already been incorrectly set and needed "correcting". However, this does not appear to be the case as the OP had already stated that requests for these supposedly "static resources" were being rewritten to index.php?route= (like the other URLs) - when it should not be being rewritten at all. The desired behaviour is that these "static resources" should be ignored by the front-controller.
    – MrWhite
    Jan 17, 2020 at 2:59
0
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Providing the static resources are correctly referenced with root-relative (or absolute) URLs in the HTML source (and there is no conflicting BASE element) then these conditions should already exclude the static resources from being rewritten.

You could add a condition to your front-controller that excludes requests for specific file extensions (assuming your URLs that require rewriting do not also share these file extensions).

This would also be a recommended optimisation for any front-controller as it prevents unnecessary filesystem checks for static resources - which will simply default to a 404 if not found (rather than being routed through the framework).

For example:

RewriteEngine On

RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|png|gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?route=$1 [L,QSA]

Only when the requested URL does not end in one of the stated file extensions will it perform a filesystem check and potentially rewrite the URL to index.php (the front-controller).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.