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条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系