.htaccess pulling text to left

后端 未结 2 1939
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 04:30

So the problem is, when I use only the first parameter, the page works like a charm, but when I use the second one, my CSS gets messed up (text alignment -> left)



        
相关标签:
2条回答
  • 2021-01-22 05:25

    This is how I did it. Basically when you will pull the URL of resources, the check will do perform empty check and result nothing.

    I had similar problem when my style url would direct me to the the php handler page, so this is what I assume happens to you.

    RewriteCond %{REQUEST_FILENAME} !-d # avoiding directory
    #RewriteCond %{REQUEST_FILENAME} !-f # not checking if is a file, commented out
    # must be located before all other rules
    RewriteRule ^([.+/]*.*[.+].+)$ - [L] # file must be skipped
    
    RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?mode=$1&user=$2 [L]
    RewriteRule ^([\w-]+)/?$ index.php?mode=$1 [L]
    
    0 讨论(0)
  • 2021-01-22 05:27

    That's because you're using relative paths instead of absolute paths for all your html links (images, javascript, css, href links).

    Actually, your rules create virtual directories.
    That's why i'm surprised dummy.com/mode/ (with trailing slash) also works.

    Let's say you have css links that way

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

    For all your examples, here is the path resolution

    • dummy.com/mode -> /css/style.css
    • dummy.com/mode/ -> /mode/css/style.css
    • dummy.com/mode/user -> /mode/css/style.css
    • dummy.com/mode/user/ -> /mode/user/css/style.css

    Can you see the problem now ?
    To avoid that behaviour, use absolute path

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

    Or, if you don't want to change all your html links, you can add this line after <head> html tag

    <base href="/">
    

    Note 1: assuming for absolute path that everything was in root folder.

    Note 2: you should add a RewriteBase in your htaccess (to avoid same problem with virtual directories)

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?mode=$1&user=$2 [L]
    RewriteRule ^([\w-]+)/?$ index.php?mode=$1 [L]
    

    Note 3: you should avoid /? in the end of your rules (which means / is optional) because it creates 2 different urls with same content (this is called duplicate content and that's not good for search engines).

    Make a choice: with or without trailing slash but not both.

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