繁体   English   中英

htaccess重写规则失败

[英]htaccess rewrite rule fails

.htaccess对我来说就像是火箭科学(我想我不是唯一的人)。 我正在尝试创建一些RewriteRule来美化我的URL。

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这样的URL会自动变为/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错误文档。

我发疯了,因为我希望该URL: http://localhost/students/?show_profile=123#photos成为http://localhost/students/123/#photos (其他URL依此类推)

我的问题

  1. 我在哪里错了,我的.htaccess文件应该看起来如何?
  2. 有没有写得很好的参考书或书会教我如何使用和编写.htaccess

重新排列规则,使用选项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简介

Apache mod_rewrite技术细节

Apache mod_rewrite深入细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM