【问题标题】:php url rewriting in .htaccess.htaccess 中的 php url 重写
【发布时间】:2013-05-23 18:19:01
【问题描述】:

我已尽我所能,但无济于事,检查了 url .htaccess 重写上的所有网络教程,没有一个专门解决我的所有问题,我的编程专家帮助我解决这个问题,我有一个 php web 应用程序,.htaccess代码我只管理没有.php扩展名的url,比如localhost/app/images.php,链接是localhost/app/images,一旦你点击它,.htaccess就会理解/images/images.php并获取文档,但是当我试图多放一些 .htaccess 代码来重写一些动态链接,比如/images/miscellaneous,正确的链接应该在哪里

/images.php?album_id=miscellaneous

我明白了

internal server error

这是我现在拥有的 .htaccess 代码,它只匹配 /images 到 /images.php

# Turn on URL rewriting
RewriteEngine on
RewriteBase /ansjc
# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule images/album_id/(.*)/ images.php?album_id=$1
RewriteRule images/album_id/(.*) images.php?album_id=$1 

【问题讨论】:

    标签: php apache url rewrite


    【解决方案1】:

    你的重写规则有两个主要问题:

    • 它们的顺序很重要。现在,你的第二个和第三个永远不会匹配的东西
    • 其中两个可以简化为一个。

    考虑使用这个:

     RewriteEngine on
     RewriteBase /ansjc
     # Remove file extension
     RewriteRule images/album_id/(.+)/?$ images.php?album_id=$1 [L]
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule (.+) $1.php [L]
    

    [L] 标志表示“最后一个”。换句话说,如果匹配,则不会在重写方面进行任何其他处理。所以,如果 images/album_id/etc/ 被匹配,第二个重写规则不会干扰。

    这也解决了 .php 被附加到所有内容的问题。虽然我怀疑您的 500 可能来自您的代码,而不是来自重写。

    【讨论】:

  • @NwachukwuAlNnaemeka:请记住,images.php 突然不能神奇地理解它现在比以前低了两个目录。如果您的样式表和图像是相对指定的(href="css/styles.css"),您需要将它们调整为绝对的。
  • 【解决方案2】:

    使用此代码

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^([^/]*)$ $1.php [NC,L]
    RewriteRule ^images/(.*)$ images.php?album_id=$1 [L]
    

    试试看

    http://localhost/images
    http://localhost/images/
    http://localhost/images/album_id
    

    它将调用images.php,而在images.php中只是 print_r($_GET);测试。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签