简体   繁体   English

Apache Rewrite URL创建链接问题

[英]Apache Rewrite URL Creating Linking Issues

Problem: rewrite url causing some links to break. 问题:重写URL导致某些链接中断。

.htaccess has below rule: .htaccess具有以下规则:

RewriteRule ^blog/([0-9]+)/[-0-9a-zA-Z]+$ index.php?action=blog&postID=$1\\%23disqus_thread [NC] RewriteRule ^ blog /([[0-9] +)/ [-0-9a-zA-Z] + $ index.php?action = blog&postID = $ 1 \\%23disqus_thread [NC]

Style sheet reference in header template: 标题模板中的样式表参考:

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

I can click on: 我可以点击:

domain.com/blog/1/title-of-article and get to the file just fine, but style sheet link breaks domain.com/blog/1/title-of-article并可以正常访问文件,但是样式表链接中断

If I go directly to: 如果我直接去:

domain.com/index.php?action=blog&postID=1#.UYV1mcqRiSo then the style sheet loads fine (ignore #.UYV1mcqRiSo, that is code from Disqus) . domain.com/index.php?action=blog&postID=1#.UYV1mcqRiSo然后样式表会正常加载(忽略#.UYV1mcqRiSo,这是来自Disqus的代码)

This is also breaking my logo link, which is: 这也破坏了我的徽标链接,即:

<a href="./">

Instead of taking me to domain.com, it's going to domain.com/blog/1/ 而不是带我到domain.com,而是转到domain.com/blog/1/

My basic file structure is: 我的基本文件结构是:

index.php and style.css is in root, which loads up viewPost.php in/templates folder. index.php和style.css在根目录中,这会在/ templates文件夹中加载viewPost.php。

What is going and how do I correct this? 怎么回事,我该如何纠正?

1. About the stylesheet link 1.关于样式表链接

You have to include these conditions in your .htaccess before the rewrite rule: 在重写规则之前,必须在.htaccess中包括以下条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

The first means that rewrite rules work only if the requested URL is not to an existing file, and the second does the same for existing directories. 第一种意味着重写规则仅在请求的URL 不在现有文件中时才起作用,第二种意味着对现有目录也是如此。 Without these conditions the request for style.css is also passed to your index.php file, not to the style.css file directly. 如果没有这些条件,对style.css的请求也将传递到您的index.php文件,而不是直接传递到style.css文件。

If you have these rules already and it still doesn't work then the problem is with URL paths, see below. 如果您已经有这些规则,但仍然无法使用,则问题出在URL路径上,请参见下文。

2. About URL paths 2.关于URL路径

You're using relative URLs in your links, both in the <a href="./"> and in the <link> tag. 您在链接中使用相对URL,在<a href="./"><link>标记中。 In this case they are resolved to the current directory of the URL (it doesn't matter that it's rewritten to the same file serverside, the path is prepared on the client side and every part separated with a slash / is treated as a directory. Thus if the current URL is domain.com/blog/1/ , ./ is resolved to domain.com/blog/1/ .) The path to style.css may be resolved to domain.com/blog/1/style.css . 在这种情况下,它们将解析为URL的当前目录(将其重写到同一文件服务器端无关紧要,路径是在客户端准备的,每个用斜杠/分隔的部分都被视为目录。因此,如果当前的URL是domain.com/blog/1/, ./被解析为domain.com/blog/1/。)所述的路径style.css可以被解析以domain.com/blog/1/style。 css If you want these links work as if they're directly after the domain name in the URL, you have to use absolute URLs, ie, / without the dot for the link on your logo; 如果您希望这些链接就像直接位于URL中的域名之后一样工作,则必须使用绝对URL,即/徽标上的链接不带点; /style.css for the stylesheet link. /style.css以获得样式表链接。

Easiest solution: Just set your links relative to the domain root, by fronting them with a slash (resp. removing the dot referring to the current folder in the link): 最简单的解决方案:只需相对于域根设置链接,只需在它们前面加一个斜杠即可(分别删除指向链接中当前文件夹的点):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM