doujiao6507 2013-08-29 16:09
浏览 14
已采纳

在.htaccess中使用重写mod

I'd like to write a rewrite rule to do the following: if any of the following files exist on the server as a static html file in a specific directory then I'd like .htaccess to serve that static file. Otherwise, I'd like the id (first number after the .com/ and before the first hyphen) to be passed as a query parameter to www.gallery.com/index.php

Redirect should occur for the following URLs if it doesn't exist as a static HTML page.

 www.gallery.com/2-swimming.html
 www.gallery.com/2
 gallery.com/2

Below is my entire .htaccess file

<Files .htaccess> 
   order allow,deny 
   deny from all 
</Files> 


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [L]
</IfModule>

Is there anything wrong with my rewrite condition.(I'm having a hard time). Also what is an efficient way to grab the id incase of a redirect.

  • 写回答

1条回答 默认 最新

  • donglei2288 2013-08-29 16:12
    关注

    Why not just always pass everything to index.php?

    PHP is a lot better at manipulating strings than .htaccess

    The -f line already takes care of existing static files. (And -d for directories)


    RewriteRule ^(.*)$ /index.php?id=$1 [L,QSA]
    

    This rule matches and captures everything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan