dtpngq3378499 2012-09-13 10:30
浏览 103
已采纳

如果文件存在则重写扩展名,如果文件不存在则重定向,使用htaccess

Using .htaccess, I'd like to redirect files that do not exist to a controller page, and rewrite the extension of .php files that do exist to an .html extension. If a file exists and is an .html page, I'd like it to remain the same. Every time I try to inject the rewrite rule from .php to .html, I seem to mess up the redirect to the controller page. So I'm not sure where to go from here:

Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>

Any help I'd be most grateful for.

Edit

I seem to have found most of the answer here (but I have to leave out the ReweriteBse or it doesn't work). The biggest issue is that now, my existing .html files don't work, it only serves my .php files with .html extensions and directs all else to the controller. Existing .html files go to my 404 page. I'd like to know how I can keep my existing .html files intact. My new code as follows:

  RewriteEngine on

  RewriteCond %{THE_REQUEST} (.*)\.php  
  RewriteRule ^(.*)\.php $1.html [R=301,L]  

  RewriteCond %{THE_REQUEST} (.*)\.html  
  RewriteRule ^(.*)\.html $1.php [L]  

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ mycontroller.php [L,QSA]
  • 写回答

3条回答 默认 最新

  • dqhr76378 2012-09-15 03:29
    关注

    I'm sure there are better ways to do this, but the following works for my needs. The other answers provided didn't seem to work despite my attempts.

    Options -Indexes
    Options +FollowSymLinks
    DirectoryIndex index.php
    ErrorDocument 404 /404.php
    
    <IfModule mod_rewrite.c>
      RewriteEngine on
    
      # Check if .html file already exists -- if so, do nothing
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteCond %{THE_REQUEST} (.*)\.html  
      RewriteRule ^.*$ - [NC,L] 
    
      # Check if .php file already exists -- if so, rewrite extension to .html
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteCond %{THE_REQUEST} (.*)\.php  
      RewriteRule ^(.*)\.php $1.html [R=301,L]  
    
      RewriteCond %{THE_REQUEST} (.*)\.html  
      RewriteRule ^(.*)\.html $1.php [L]  
    
      # All else goes to the controller page
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^(.*)$ mycontroller.php [L,QSA]
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错