CSS not loading after redirect with htaccess rewrite rule

前端 未结 12 2215
自闭症患者
自闭症患者 2020-12-03 05:18

I have the following Short-hand for a user profile url

RewriteRule ^(\\w+)/?$ ./profile.php?name_of_user=$1

The site is styled with the app

相关标签:
12条回答
  • 2020-12-03 05:53

    Try this:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /public/$1.$2 [L,NC]
    
        # Removes index.php from ExpressionEngine URLs  
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
    
    </IfModule>
    
    0 讨论(0)
  • 2020-12-03 05:56

    the easiest way to fix this is to specify the full path to your CSS file in the <head> of the HTML file.

    If your mod_rewrite rule is modifying the path to where your CSS file is, you'd also want to place this before your RewriteRule:

    RewriteCond %{REQUEST_FILENAME} !-f
    

    This makes sure that the request doesn't match a file before rewriting it.

    0 讨论(0)
  • 2020-12-03 05:58

    I had the same problem and I've solved it with the following code. Not the best solution but it works.

    RewriteRule ^(something/)$ something [R] 
    RewriteRule ^(something)$ index.php?page=$1 [L]
    
    0 讨论(0)
  • 2020-12-03 05:59

    Try to set right basedir at your html documents head section:

    <head>
      <base href="http://example.com/">
    <head>
    

    Then any extra slashes (/) at your .htaccess rules won't change exterial sources path in your html documents. The following rule

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(\w+)/?$ ./profile.php?name_of_user=$1 
    

    won't make this behaviour with <base href="http://example.com/"> at document's <head>. It will works fine, especially for relative site file structures.

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

    The easiest way is to use <base href="site url here">, and this should be soon after the opening HTML head tag. Site url needs to start with http or https; if you are on localhost then use http, for example

    http://localhost/road/ 
    
    0 讨论(0)
  • 2020-12-03 06:06

    When your page URL is example.com/name_of_user/, loading of resource css/js/images may cause problems if your HTML page is using relative paths for these resources. It is because browser resolves resource URLs using current URL.

    So for example if a style tag is:

    <link rel="stylesheet" type="text/css" href="static/style.css">
    

    and your current page URL is:

    http://example.com/name_of_user/
    

    Then browser will resolve css URL as as example.com/name_of_user/static/style.css instead of example.com/static/style.css. This will cause 404 for resource URLs and your page will be displayed without any style, scripts and images.

    You can solve this problem using one of the following ways:

    1. Use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

    2. Otherwise You can add this just below <head> section of your page's HTML:

      <base href="/" />
      

    so that every relative URL is resolved from that base URL and not from the current page's URL.

    0 讨论(0)
提交回复
热议问题