繁体   English   中英

.htaccess中的php URL重写

[英]php url rewriting in .htaccess

我已经尽力而为,尝试了所有关于url .htaccess重写的网络教程,没有一个解决了我所有的问题,我的编程专家帮助了我,我有一个php web应用程序,只有.htaccess代码管理没有.php扩展名的url,例如localhost/app/images.php ,链接是localhost/app/images ,一旦单击它,.htaccess就会理解/images/images.php并获取文档,但是当我尝试添加更多.htaccess代码来重写一些动态链接,例如/images/miscellaneous ,其中正确的链接应位于

/images.php?album_id=miscellaneous

我得到

internal server error

这是我现在拥有的.htaccess代码,它仅将/ images与/images.php匹配

# Turn on URL rewriting
RewriteEngine on
RewriteBase /ansjc
# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule images/album_id/(.*)/ images.php?album_id=$1
RewriteRule images/album_id/(.*) images.php?album_id=$1 

您的重写规则有两个主要问题:

  • 它们的顺序很重要。 现在,您的第二和第三名将永远不会匹配
  • 其中两个可以简化为一个。

考虑使用此:

 RewriteEngine on
 RewriteBase /ansjc
 # Remove file extension
 RewriteRule images/album_id/(.+)/?$ images.php?album_id=$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule (.+) $1.php [L]

[L]标志表示“最后”。 换句话说,如果匹配,则不会进行任何其他重写操作。 因此,如果images / album_id / etc /被匹配,第二个重写规则将不会受到干扰。

这也解决了将.php附加到所有内容的问题。 尽管我怀疑您的500可能来自您的代码,而不是来自重写。

使用此代码

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]*)$ $1.php [NC,L]
RewriteRule ^images/(.*)$ images.php?album_id=$1 [L]

并尝试

http://localhost/images
http://localhost/images/
http://localhost/images/album_id

它将调用images.php并在images.php内部调用print_r($ _ GET); 去测试。

暂无
暂无

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

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