的.htaccess重写“/book.php?id=1234”到“/电子书/ 1234”的.htac

2019-05-12 11:54发布

这是我到目前为止在我的开发环境:

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.

我在想什么,为什么重定向循环?

Answer 1:

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

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

被添加查询字符串参数。

下面的重写规则将改写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]


Answer 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 

你做。



文章来源: .htaccess rewrite “/book.php?id=1234” to “/book/1234”