【问题标题】:Rewritting PHP URL using .htaccess使用 .htaccess 重写 PHP URL
【发布时间】:2019-12-17 13:34:45
【问题描述】:

我有一个格式为

的 URL

motors.com/project?id=1&title=british-car-auctions

并希望将其更改为

motors.com/project/1/british-car-auctions

我使用了来自URL rewriting with PHP 和Rewritting URL by htaccess 的解决方案,但没有成功。至于 URL 的 title 部分,我用数据库中的破折号来调用这个确切的文本,这应该不是问题。这个问题有什么可能的解决方案吗?

# mode_rewrite starts here

RewriteEngine on

# does apply existing directories, meaning that if the foilder exists 

RewriteCond %{REQUESTED_FILENAME} !-d

# check for file in directory with .hmtl extension

RewriteCond %{REQUEST_FILENAME}\.php -f

# here we actually show the page that has the .html extension

RewriteRule ^(.*)$ $1.php [NC,L]

#-----------------------------------------------------

RewriteCond %{REQUESTED_FILENAME} !-d

# check for file in directory with .hmtl extension

RewriteCond %{REQUEST_FILENAME}\.html -f

# here we actually show the page that has the .html extension

RewriteRule ^(.*)$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^news/([0-9]+)(/?)$/([0-9a-zA-Z\s_]+) news/article.php?id=$1&title=$2 [NC,L]

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    最终找到了解决方案。 url 重命名与 .php 文件名不同。

    所以而不是:

    RewriteRule ^news/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [NC,L]
    

    把php改成这个就解决了问题:

    RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [NC,L]
    

    另外,我的 html 中的 php 代码必须以与我在 .htaccess 文件中所写相同的格式重新编写

    再次相同,而不是:

    <a class="brand-text" href="article?id=<?php echo $pizza['id']?>&title=<?php echo ($pizza['slug'])?>">
    

    我改成

     <a class="brand-text" href="article/<?php echo $pizza['id']?>/<?php echo ($pizza['slug'])?>">
    

    希望这对未来的人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2022-01-08
      • 2012-04-29
      • 2012-08-25
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多