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.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c