的.htaccess拉文本到左文本、htaccess

2023-09-02 00:23:33 作者:天黑怕被劫色

所以,问题是,当我只使用了第一个参数,该页面用得好好的,但是当我用的是第二个,我的CSS得到弄糟(文字对齐 - >左)

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)

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

dummy.com/mode dummy.com/mode/ 作品!的

dummy.com/mode dummy.com/mode/ Works!

dummy.com/mode/user dummy.com/mode/user/ 对齐文本向左!的

dummy.com/mode/user dummy.com/mode/user/ Aligns text to left!

推荐答案

那是因为你使用的相对路径,而不是绝对您所有的HTML链接路径(图像,JAVASCRIPT,CSS,HREF链接)。

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

其实,你的规则创建虚拟目录。 这就是为什么我很惊讶 dummy.com/mode / (带斜杠)也适用。

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

让我们假设你有CSS链接方式

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 的 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">

或者,如果你不想改变你所有的HTML链接,你可以加入这一行后,&LT; HEAD&GT; HTML标记

<base href="/">

注1 :假设绝对路径,一切都在根文件夹中。

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

注2 :你应该添加一个的RewriteBase 在你的htaccess(以避免同样的问题与虚拟目录)

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]

注意3 :你应该避免 / 在你的规则结束(即 / 是可选的),因为它创建2个不同的网址具有相同的内容(这被称为重复的内容,而不是为搜索引擎好)。

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).

进行选择:带或不带斜线而不是两个的