.htaccess将“/book.php?id=1234”重写为“/ book / 1234”

时间:2012-07-27 23:03:19

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

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

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的错误.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.

我缺少什么,为什么重定向循环?

2 个答案:

答案 0 :(得分:4)

您写的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]

答案 1 :(得分:1)

尝试一下!

# 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 

您完成了。