doutian4046 2012-07-02 16:17
浏览 149
已采纳

使用.htaccess重定向404内容的规则

It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.

Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • doumo6356 2012-07-02 17:56
    关注

    You could do something like

    Edited based on comments:

    RewriteEngine On
    
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule dev/(.*)$ $1 [R=301,L]
    

    Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.

    This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example

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

报告相同问题?

悬赏问题

  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛