首页  >  问答  >  正文

javascript - htaccess rewrite 的问题

我有个thinkphp开发的网站,企用了mod rewrite

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteRule ^(.*)$ index.php/ [QSA,PT,L]

</IfModule>

thinkphp 和程序运行正常,现在我的需求是,我在网站根目录的hot目录下,放了一个vue开发的小程序,并开启了histroy路由模式,
按照开发文档说明 为了让所有的路径都可以被服务器识别,也要通过RewriteRule 把所有的访问 Rewrite到 index.hml上

也就是说要把http://wwww.xxx.com/hot/item/1000 这样的url 解析到/hot/index.html上,
其它还是用thinkphp来控制,
试过了好多配置 都不行,本人对.htaccess文件命令不熟悉,所以希望大家给个方案

回复一楼的内容,我也贴一下这里
vue 运行也是正常的,在开启histroy 模式下,你进入http://www.xxx.com/hot可以正常运行,点击各个链接也是可以正常跳转,因为html5的histroy模式只是在浏览器段改变了url地址栏,并没有向server请求数据,但是当用户直接用 http://www.xxx.com/hot/item/1000 这样的url来访问的时个,由于server上并没有这个文件 所以不会正常返回,但是通过apache的htaaccess文件可以把用户访问不存在的文件跳转到指定的文件上,就如我上面贴的thinkphp下的配置文件就是起这个作用,我现在的要求是把在/hot目录下的请求 不要转发给thinkphp来执行,而是转发个/hot/index.html来处理。

我尝试如下

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  
  RewriteCond %{REQUEST_URI} ^/(hot|hot/.*)$
  RewriteRule ^/hot/index\.html$ - [L]

  RewriteRule ^(hot|hot/.*)$ hot/index.html$ [L]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteRule ^hot/(.*)$ hot/index.html$
  #RewriteRule ".?" "-" [S=1]
  #RewriteRule ^/(hot|hot/.*)$ /hot/index.html$ [L]
  RewriteRule ^(.*)$ index.php/ [QSA,PT,L]

</IfModule>

但是不起作用,不知道为什么?

phpcn_u1582phpcn_u15822713 天前433

全部回复(2)我来回复

  • 为情所困

    为情所困2017-05-16 16:59:11

    对 thinkphp 不了解,可以看一下 thinkphp 的路由配置可否指定某一个目录不同的路由方案

    如果不行可能要用 nginx 了

    回复
    0
  • PHP中文网

    PHP中文网2017-05-16 16:59:11

    解决了,原来的我想法是对的,一开始老是被解析到主目录的index.php原来是我的引用资源路径的问题
    贴出有效 配置

    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
      
      RewriteCond %{REQUEST_URI} ^/(hot|hot/.*)$
      RewriteRule ^/hot/index\.html$ - [L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(hot|hot/.*)$ hot/index.html [L]
    
      RewriteCond %{REQUEST_URI} !^/(hot|hot/.*)$
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php/ [QSA,PT,L]
    
    </IfModule>

    测试地址: http://www.wx2share.com/hot/
    如果不做url rewrite 直接通过 以上网址进入,一切功能也是正常的,

    但是如果通过如下网址进入
    http://www.wx2share.com/hot/i...
    就会返回 404,但是做了 url rewrite 就可以正常访问了

    回复
    0
  • 取消回复