How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?

前端 未结 4 1787
無奈伤痛
無奈伤痛 2020-12-09 11:29

I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.

However, it\'s not al

相关标签:
4条回答
  • 2020-12-09 12:01

    RewriteCond %{REQUEST_FILENAME} !-f alone should be enough.

    Another option is t exclude specific files from the rewrite. From the TYPO3 packages:

    # Stop rewrite processing, if we are in the typo3/ directory.
    # For httpd.conf, use this line instead of the next one:
    # RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
    RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
    

    This rule should appear before your actual rewrite. It should be

    RewriteRule ^(public/|*\.css|*\.js|*\.png|*\.jpg|*\.gif|robots\.txt) - [L]
    

    in your case.

    0 讨论(0)
  • 2020-12-09 12:06

    You have the right idea, tweaking the fourth line. The ^ is saying the various matching strings must be at the beginning of the line. If you don't care where any of these appear in the file, you can just remove the ^. That will avoid rewriting *.css, *.js, etc.; but will also not rewrite publicideas.html.

    If you want to limit to just the suffixes, try this:

    RewriteCond $1 !^(index\.php|public|.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.gif|robots\.txt)$
    

    This says to match anything at the beginning, then a ., then the suffix. The $ says match these at the end (nothing following).

    I'm not sure about the public, so I left it (which means exactly public, with nothing else - probably not what you meant, but you can add a * before or after, or both).

    0 讨论(0)
  • 2020-12-09 12:08

    The easiest is to ignore them explicitly early in your rules:

    RewriteRule \.(css|js|png|jpg|gif)$ - [L]
    RewriteRule ^(index\.php|robots\.txt)$ - [L]
    

    This avoid carrying them around all over the place with RewriteCond.

    At your option, check that the file exists prior to doing so:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule \.(css|js|png|jpg|gif)$ - [L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(index\.php|robots\.txt)$ - [L]
    

    (Note that the file check generates an extra disk access.)

    0 讨论(0)
  • 2020-12-09 12:24

    Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:

    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    

    This should work by itself since any of those files *should* be real files.

    ErrorDocument 404 /index.php    
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    

    To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:

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

    If you're trying to ignore a folder then you could add:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !(public|css)
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    
    0 讨论(0)
提交回复
热议问题