【问题标题】:.htaccess rewrite "/book.php?id=1234" to "/book/1234".htaccess 将“/book.php?id=1234”重写为“/book/1234”
【发布时间】:2012-07-26 15:47:50
【问题描述】:

这是我目前在我的开发环境中所拥有的:

php_value error_log log/php.log
php_value display_errors 1
php_value magic_quotes_gpc Off

RewriteEngine On

# remove slash
RewriteCond %{REQUEST_FILENAME} !-d

# file.php to /file
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteCond %{REQUEST_URI} !^/ajax.php
RewriteRule ^(.+)\.php$ http://localhost/$1 [R=301,L]

# /file to file.php
RewriteRule ^([^/.]+)$ $1.php [L]

我尝试了很多此规则的变体,但总是收到 500 错误:

RewriteRule ^(.*)$ /book.php?url=$1 [L]

这是在我的 Apache 的 error.log 中:

[Fri Jul 27 19:56:51 2012] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

我错过了什么,为什么会出现重定向循环?

【问题讨论】:

  • 另外,有没有办法让规则完全独立于域?我想避免在本地/qa/prod 环境中更改此文件
  • 或者只是RewriteRule ^book/([^/]*)$ /book.php?id=$1 [L]。最后但同样重要的是,尝试添加:RewriteBase /

标签: apache .htaccess mod-rewrite url-rewriting redirect-loop


【解决方案1】:

您所写的RewriteRule 似乎试图与您的问题标题所说的相反。你说你想要htaccess rewrite /book.php?id=1234 to /book/1234,但是你的RewriteRule

RewriteRule ^(.*)$ /book.php?url=$1 [L]

正在添加查询字符串参数。

下面的 RewriteRule 将重写rewrite /book.php?id=1234 to /book/1234

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^book\.php$ /book/%1? [L]

另外,如果你真的想走另一条路,即将/book/1234 重写为/book.php?id=1234,下面应该这样做:

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

【讨论】:

    【解决方案2】:

    试试这个!

    # in htaccess file write this
    RewriteEngine on 
    
    RewriteRule ^book/([0-9]+) book.php?id=$i
    
    # and in you html or php file call
    
    book.php?id=$i 
    

    逐页

    book/$i 
    

    你已经完成了。

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2015-11-20
      • 2019-06-29
      • 2012-08-30
      • 2015-01-30
      相关资源
      最近更新 更多