使用 RewriteRule 后不加载样式表并包含

2021-12-21 00:00:00 php html css mod-rewrite

我有一个网站,它有一个 index.php、几个简单的 .html 页面和一个 .php 页面(联系表格)以及一个 .htaccess 文件.在我的网站上,我有指向页面的按钮,如下所示:index.php?p=welcome.php(示例).如您所见,index.php 文件 includes 一个页面,具体取决于 GET 传递的内容.

I have a website that has an index.php, a few simple .html pages and one .php page (contact form) as well as a .htaccess file. On my website I have buttons that point to the pages as follows: index.php?p=welcome.php (example). As you can see, the index.php file includes a page, depending on what was passed on by GET.

我写了 .htaccess 来显示:http://www.site.com/welcome/ 而不是 http://www.sites.com/index.php?p=欢迎.链接和重写都有效.我遇到的唯一问题是当我单击其中一个按钮导航到另一个页面时:没有任何样式.

I've written the .htaccess to show: http://www.site.com/welcome/ instead of http://www.sites.com/index.php?p=welcome. The links and rewrite both work. The only problem I have is when I click one of the buttons to navigate to another page: there is no styling whatsoever.

我已经测试过 index.php 文件是否正确地包含了其他页面,并且确实如此.只是样式表似乎不起作用.style.css 包含在它所属的 index.php 文件的顶部.以下是我想向您展示的一些代码:

I've tested to see if the index.php file is including the other pages properly, and it does. It's just the stylesheet that doesn't seem to work. The style.css is included at the top of the index.php file, where it belongs. Here are bits of code I'd like to show you:

按钮

<table>
    <tr>
        <td><a href="/welcome/"><div>Welcome</div></a></td>
    </tr>
    <tr>
        <td><a href="/references/"><div>References</div></td></a>
    </tr>
    <tr>
        <td><a href="/aboutme/"><div>About me</div></td></a>
    </tr>
    <tr>
        <td><a href="/contact/"><div>Contact</div></td></a>
    </tr>
</table>

.htaccess

RewriteEngine On
RewriteRule ^([a-z]+)/$ index.php?p=$1

需要说明的是:该站点仍然像往常一样运行,但它只是不应用样式表中的任何样式.任何帮助将不胜感激.

To make it clear: the site still functions as it normally does, but it just does not apply any styling from the stylesheet. Any help would be much appreciated.

推荐答案

如果使用相对路径包含样式表,那么这就是问题所在.尝试使用:/path/to/style.css 而不是 relative/path/to/style.css

If the stylesheet is being included using a relative path, then that would be the problem. Try using: /path/to/style.css rather than relative/path/to/style.css

因此,如果您的样式表位于 templates/style.css 中并且您如此引用它,那么当您重写 URL http://www.site.com/index.php 时?page=welcomehttp://www.site.com/welcome/,样式表的路径是相对的:

So if your stylesheet lives in templates/style.css and you reference it as such, when you rewrite the URL http://www.site.com/index.php?page=welcome to http://www.site.com/welcome/, the stylesheet's path is relative to that:

http://www.site.com/welcome/templates/style.css

相关文章