URL重写在末尾添加斜杠会破坏我的CSS链接 [英] URL Rewriting adding slash at the end breaks my css links

查看:58
本文介绍了URL重写在末尾添加斜杠会破坏我的CSS链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache mod_rewrite在PHP应用程序中重写我的URL. 我的应用程序根目录中有一个login.php. 我在.htaccess文件中写了以下几行(我正在使用HTML5样板文件的htaccess):

I'm using Apache mod_rewrite to rewrite my url's in a PHP application. I have a login.php in my application root. I wrote the following lines in .htaccess file (I'm using HTML5 boilerplate's htaccess):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule  ^signin/?$  login.php
</IfModule>

在名称 ^ signin/?$ 的末尾添加斜杠会破坏此页面的所有CSS链接.

adding a slash at the end of the name ^signin/?$ broke all the css links of this page.

我正在使用CSS的相对链接,例如:

I'm using relative links for css there such as:

<link href="css/bootstrap-reset.css" rel="stylesheet">

我对URL重写和htaccess之类的东西很陌生,因为我正在学习所有这些知识,所以我想知道为什么会发生这种行为?

I'm very new to URL rewriting and htaccess stuff, since i'm learning all this i would love to know why is this behavior occurring?

推荐答案

一种方法是拥有一个新的重定向规则来删除尾部斜杠,然后您的css/js不会出现问题:

One way is to have a new redirect rule to remove trailing slash and then your css/js would not be a problem:

RewriteEngine On
RewriteBase /apx/

# remove trailing slash from non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [NE,R=302,L]

RewriteRule ^signin/?$ login.php [L,NC]

但是,也请考虑将绝对路径用于css/js,例如

However also consider using absolute paths for css/js e.g.

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

否则,您可以在页面HTML的<head>部分中添加此代码:

Or else you can add this in the <head> section of your page's HTML:

<base href="/apx/" />

这篇关于URL重写在末尾添加斜杠会破坏我的CSS链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆