.htaccess pulling text to left

后端 未结 2 1940
隐瞒了意图╮
隐瞒了意图╮ 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: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

    
    

    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

    
    

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

    
    

    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.

提交回复
热议问题