dongmei9203 2014-05-30 15:43
浏览 41
已采纳

htaccess重写规则相同的域名

I have a url rewrite issue on the .htaccess file. There are 2 rules on the file:

rule 1 // When user open "example.com/test.php" on a browser then user will be redirected to "example.com/test".

htaccess rule: RewriteRule ^test.php(.*)$ http://example.com/test/$1 [r=301,nc]

rule 2 // When user will open "example.com/test" we will retrieve the content from "example.com/test.php".

htaccess rule: RewriteRule ^test/ test.php [NC]

Right now those rules are not working, maybe there is a conflict somewhere. What rewrite rules i should use for the above conditions?

  • 写回答

1条回答 默认 最新

  • douwei7203 2014-05-31 00:16
    关注

    The two rules conflict with one an other. One writes test.php to test and the other writes test to test.php, which is likely to leave you in an infinite loop, either within the rewrite engine or between the browser and server depending on how you implement the rewrite.

    To solve this I've used a condition to check the requested URI rather than the URI as its currently rewritten.

    Try this:

    RewriteRule ^test/$ test.php [NC]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^\ ]*)\.php[?\ ]
    RewriteRule ^test\.php(.*)$ http://example.com/test/$1 [R=301,NC]
    

    Couple of things to note that might also be factors:

    1. You hadn't escaped the dot in test.php in your first rewrite
    2. The Apache MultiViews directive can get in the way and do some rewriting before you get your hands on the on the URI in .htaccess. If you have file test.php, but a request is just made to http://example.com/test, then your htaccess will be working with test.php, not just test as you might expect.
    3. Browsers cache redirects which can make it difficult to diagnose problems
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表