【问题标题】:page slug with rewriterule in htaccess在 htaccess 中使用 rewriterule 的页面 slug
【发布时间】:2015-02-24 02:32:12
【问题描述】:

我在 htaccess 中使用 Rewrite Rule 来通过 slug 获取页面数据。

我是通过在 .htaccess 中添加行来手动完成的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^עמוד-מסוים$ /page.php?id=1 [L]
RewriteRule ^דוגמא-לעמוד$ /page.php?id=2 [L]
RewriteRule ^דוגמא-נוספת$ /page.php?id=3 [L]
RewriteRule ^טקסט-כלשהו$ /page.php?id=4 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]

效果很好,但我每次都需要添加新的 .htaccess 行。

我的数据库表有 Id 和 Slug(以及其他信息) 我的 page.php 使用 $_GET['id'] 然后通过这个 ID 从 DB 中选择其他数据。

我尝试过这样的事情:

.htaccess:

RewriteRule ^page/([^/]*)$ /page.php?id=$1 [L]

page.php:

$id=$_GET['id'];

    if(is_int($id)==false){ // if slug was enterd
        $result=$mysqli->query("SELECT `id` FROM `pages` WHERE  `slug`='$id' limit 1");
        while($row=mysqli_fetch_assoc($result)){
            $id=$row['id'];
        }//query
    }

但 URL 看起来不像我想要的(将 /page/ 添加到 url)

我也试过了:

.htaccess:

RewriteRule ^/([^/]*)$ /page.php?id=$1 [L]

但是我网站上的其他 url 有问题(没有连接到 page.php)

如何在不每次都添加 htaccess 行的情况下使其工作?

编辑: 当我尝试这个 .htaccess 时:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]

我在所有页面上都出现错误,如果我向上移动 RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L] 只有 page.php 文件可以正常工作(博客和联系方式无效),非 WWW 网址也无效。

【问题讨论】:

    标签: php .htaccess mod-rewrite redirect slug


    【解决方案1】:

    您可以尝试添加一些条件,例如:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
    

    过滤掉所有现有的文件和目录。

    另外,您可能需要考虑sanitizing your $id parameter before you stick it in a query

    【讨论】:

    • 尝试此操作时出现内部服务器错误。
    • 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员 webmaster@example.co.il 并告知他们错误发生的时间,以及您所做的任何可能导致错误的事情。服务器错误日志中可能提供有关此错误的更多信息。此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。 Apache 服务器位于 www.example.co.il 端口 80
    • 我发现了问题,但我不知道如何解决。我在你给我的那一行之前有这些行: RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC] RewriteRule ^(.*)$ example.co.il/$1 [L,R=301] If I删除它们我没有收到错误
    • @itay 您需要在 重定向规则之后放置任何路由规则,因此这些需要放在最后。您需要查看服务器日志,而不是浏览器中的错误消息。
    • 我的错误。这不是很好。如果我将 HTTP_HOST 移动到底部,它适用于 page.php,但对于不在 page.php 中的其他文件会出现问题,并且如果在没有 WWW 的情况下键入 url 就会出现问题......所以它没有解决..
    【解决方案2】:

    找到灵魂。这工作完美:

    ErrorDocument 404 /404.php 
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteRule ^צרו-קשר$ /contact.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 2013-02-17
      • 2017-11-16
      相关资源
      最近更新 更多