【问题标题】:URL rewriting in .htaccess doesn't works as it should.htaccess 中的 URL 重写无法正常工作
【发布时间】:2019-04-15 01:23:12
【问题描述】:

我目前正在 WampServer(本地)上使用 PHP 和 MySQL 编写自己的网站。我已经成功地重写了我的一些 URL。但是我遇到了其中一个问题。

我想使用 GET 方法在一个页面上显示我在一个类别中列出的所有文章。目前,我有以下 URL:http://localhost/actuco/cat.php?id=xpS3cc&slug=amerique-du-nord,我想将此 URL 使用并显示为 http://localhost/actuco/c-xpS3cc-amerique-du-nord/(它确实包含完全相同的 GET 参数比原来的网址)。

我尝试在我的 .htaccess 文件中使用以下行来做到这一点

RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2

当我在浏览器中编写第二个 URL 时,它会显示一个完全没有代码行的空白页面。我的第一个网址完美运行。

我真的迷路了,我真的不知道如何解决它。

这是我网站上使用的整个 .htaccess 文件(此文件中的所有其他 URL 重写都有效)。

Options +FollowSymlinks

RewriteEngine On

RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^bio$ o.php [L]

提前感谢您的帮助!

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    只需在您的 .htaccess 文件中添加此代码

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
    

    希望它会起作用。

    【讨论】:

    • 我应该将此添加到我的文件中还是将初始内容完全替换为您编写的内容?
    • 它根本不起作用!我以前的 URL 重写都没有工作!对不起
    【解决方案2】:

    解决了!只是忘记在这一行的 $ 之前添加斜杠

    之前:

    RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
    

    之后:

    RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
    

    现在它可以工作了,但我仍然对此处描述的 slug 中的多个连字符有疑问:Multiple hyphen handling in .htaccess (URL rewriting)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2017-08-08
      • 2014-08-25
      • 2011-05-15
      相关资源
      最近更新 更多