htaccess重写规则失败

时间:2014-09-23 07:59:41

标签: apache .htaccess mod-rewrite

对我来说,{p> .htaccess就像火箭科学一样(我想我不是唯一一个)。我正在尝试创建一些RewriteRule来美化我的网址。

RewriteEngine On
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

RewriteRule ^download/(.*)/$ /download.php?fid=$1 [L]
RewriteRule ^students/(.*)/ /students.php?show_profile=$1 [L]

PHP重定向效果很好(我的意思是,/students.php等网址自动成为/students/。现在,我特别关注这两行:

RewriteRule ^download/(.*)/$ /download.php?fid=$1 [L]
RewriteRule ^students/(.*)/ /students.php?show_profile=$1 [L]

所以,当我写:

http://localhost/students/123

它变成了:

http://localhost/C:/xampp/htdocs/students/123/

我收到一个403错误文档。

我让我疯了,因为我希望这个网址: http://localhost/students/?show_profile=123#photos http://localhost/students/123/#photos (等等其他网址)

我的问题

  1. 我错在哪里,我的.htaccess文件应该怎么样?
  2. 是否有任何精心编写的参考书或书籍可以教会我如何使用和撰写.htaccess

1 个答案:

答案 0 :(得分:1)

重新排列规则,使用选项MultiViews并修复URI模式中的一些正则表达式。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301,NE]

RewriteRule ^download/(.*)/$ download.php?fid=$1 [L,QSA]
RewriteRule ^students/(.*)/ students.php?show_profile=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

参考:Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

Apache mod_rewrite In-Depth Details