javascript - htaccess rewrite 的问题

查看:78
本文介绍了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/$1 [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/$1 [QSA,PT,L]

</IfModule>

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

解决方案

解决了,原来的我想法是对的,一开始老是被解析到主目录的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/$1 [QSA,PT,L]

</IfModule>

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

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

这篇关于javascript - htaccess rewrite 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆