如何在 php 中生成 slugs

发布于 2024-08-23 05:35:47 字数 1111 浏览 3 评论 0原文

我正在尝试编写一个脚本,它可以以 slug 方式重写网址,如下所示:

http://www.mysite .com/mystorytitle/

我在我的代码中做到了这一点:

 RewriteRule ^(.*)\/$  app=News&file=article&title=$1  [L] 

在我的 php 代码中,我根据故事标题创建了一个 slug :

$slug_title = mysql_real_escape_string($mtitle);
$show= "<a href=\"$slug_title/\">$mtitle</a>";

现在一切都很好,除非当我单击 slugged 链接时,它会转到该页面但没有任何样式、javascript 和图像。

我确信问题是由于 css 文件的路径和...它已更改了一个级别,

因为我在此路径中: http://www.mysite.com/%D8%A7%DB%8C%D9%85%DB%8C%D9%84/" mysite.com/?????/

因此,如果需要 css 文件的页面如下所示:

<link rel="StyleSheet" href="includes/NAV.css" type="text/css" />

一个级别发生了更改并且要执行此操作,我应该返回一个级别:

<link rel="StyleSheet" href="../includes/NAV.css" type="text/css" />

我猜,这被认为是一个文件夹而不是重写的路径

但我确信应该有另一种方法可以让这个脚本工作而不改变所有路径

提前感谢

im trying to write a script , that can rewrites urls in slug way such as this :

http://www.mysite.com/mystorytitle/

and i did this in my codes :

 RewriteRule ^(.*)\/$  app=News&file=article&title=$1  [L] 

and in my php codes, i created a slug out of story's title as :

$slug_title = mysql_real_escape_string($mtitle);
$show= "<a href=\"$slug_title/\">$mtitle</a>";

now everything is fine unless when i click on slugged link , it goes to the page but without any style and javascripts and images.

im sure that the problem is because of path for css files and ... which is been changed a level

as i'm in this path :
http://www.mysite.com/ایمیل/

so if the page requiring css file as this :

<link rel="StyleSheet" href="includes/NAV.css" type="text/css" />

one level changed and to do this worked , i should come back a level as :

<link rel="StyleSheet" href="../includes/NAV.css" type="text/css" />

i guess , this is concidered as a folder not a rewrited path

but im sure there should be another way to make this script work without changing all paths

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞觅青森が 2024-08-30 05:35:47

正如您已经写过的,会发生这种情况是因为浏览器认为您位于以 slug 命名的不同目录中。

您必须对 CSS URL 执行一些操作,要么像您已经做的那样相对引用它们 ../includes/,要么使用绝对引用 /includes/NAV .css(域部分是可选的)。

如果这是一个 PHP 项目,我将创建一个包含 Web 根目录的中央设置文件:

define("SITE_WEBROOT", "http://www.domain.com");

并引用与该设置相关的所有资源:

<link rel="stylesheet" type="text/css" 
      href="<?php echo SITE_WEBROOT; ?>/includes/NAV.css">

As you already wrote, this will happen because the browser thinks you are in a different directory named after your slug.

You will have to do something to the CSS URLs, either reference them relatively ../includes/ as you already do, or use absolute references /includes/NAV.css (the domain part being optional).

If this is a PHP project, I would create a central settings file containing the web root:

define("SITE_WEBROOT", "http://www.domain.com");

and reference all resources relative to that setting:

<link rel="stylesheet" type="text/css" 
      href="<?php echo SITE_WEBROOT; ?>/includes/NAV.css">
葬シ愛 2024-08-30 05:35:47

您应该在前面添加基本 URL(大多数情况下是正斜杠,但并非总是如此)。

您可以按如下方式获取它(即使您的站点驻留在目录中,通用表达式也将起作用):

$baseUrl = rtrim((string)dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

然后您将编写:

<link rel="StyleSheet" href="<?php echo $baseUrl ?>includes/NAV.css" type="text/css" />

另一种可能性是在 head 中使用基本标记:

<base href="<?php echo $baseUrl ?>" />

这具有仅更改中的一行的优点你的代码。

您可以进一步创建实用方法来执行此操作:

function baseUrl($url = '', array $query = null, $fragment = null)
{
    static $baseUrl = rtrim((string)dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

    if ($query !== null) {
        $url .= '?' . http_build_query($query);
    }

    if ($fragment !== null) {
        $url .= '#' . (string)$fragment;
    }

    $url = $baseUrl . $url;
    $url = trim(str_replace('\\', '/', (string)$url));

    return $url;
}

You should prepend the base URL (most of the time forward slash, but not always).

You can get it as follows (general expression that will work even if your site resides in a directory):

$baseUrl = rtrim((string)dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

And then you'll write:

<link rel="StyleSheet" href="<?php echo $baseUrl ?>includes/NAV.css" type="text/css" />

Another possibility is to use the base tag in head:

<base href="<?php echo $baseUrl ?>" />

This has the advantage of changing only one line in your code.

You can go furthur and make utility method to do this:

function baseUrl($url = '', array $query = null, $fragment = null)
{
    static $baseUrl = rtrim((string)dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

    if ($query !== null) {
        $url .= '?' . http_build_query($query);
    }

    if ($fragment !== null) {
        $url .= '#' . (string)$fragment;
    }

    $url = $baseUrl . $url;
    $url = trim(str_replace('\\', '/', (string)$url));

    return $url;
}
无畏 2024-08-30 05:35:47

只需在所有资源 URI 之前添加一个正斜杠...例如 /static/image.png。这将使浏览器从根目录请求它(即www.site.com/path

Just add a forward slash before all resource URIs ... like /static/image.png. That will make the browser request it from the root (ie. www.site.com/path)

柒夜笙歌凉 2024-08-30 05:35:47

您还可以在 head 部分使用基本标签,然后所有相关链接、样式表和图像都将从那里开始。我们通常这样做。它会根据请求 URL 和 index.php 的 FILE 路径在我们的所有项目中自动设置,

例如:

You can also use the base tag in your head section, then all relative links, stylesheets and images will go from there. We usually do that. It's set automatically in all our projects based on request URL and FILE path of the index.php

For example:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文