【问题标题】:adding rewriterule to htaccess ignores javascript将重写添加到 htaccess 会忽略 javascript
【发布时间】:2019-10-03 10:18:36
【问题描述】:

所以我最近一直在练习 PHP 作为一种爱好,但我无法解决这个问题。

我添加了htaccess 重写规则以使链接更容易记住或只是为了看起来更好,例如,在http://localhost/moviescat.php?id=Action 上一切正常,但在http://localhost/movies/genre/Action 上没有任何作用

我对@9​​87654331@ 知之甚少,所以我真的不知道该怎么办。一直在谷歌上搜索类似的问题,但我就是不明白htaccess。 这是我的代码:

Options -Multiviews

# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^([a-z_]+)\/?$ $1.php [NC]
RewriteRule ^user/([a-zA-Z0-9]+)$ /profile/?user_username=$1 [L]
RewriteRule ^user/([a-zA-Z0-9]+)/$ /profile/?user_username=$1 [L]
RewriteRule ^post/([^/]*)$ /post.php?id=$1 [L]
RewriteRule ^post/([^/]*)/$ /post.php?id=$1 [L]
RewriteRule ^post/([^/]*)/false$ /post.php?id=$1&false [L]
RewriteRule ^post/([^/]*)/false/$ /post.php?id=$1&false [L]

RewriteRule ^movie/([^/]*)$ /movie.php?id=$1 [L]
RewriteRule ^movie/([^/]*)/$ /movie.php?id=$1 [L]
RewriteRule ^movie/([^/]*)/false$ /movie.php?id=$1&false [L]
RewriteRule ^movie/([^/]*)/false/$ /movie.php?id=$1&false [L]

RewriteRule ^movies/genre/([^/]*)$ /moviescat.php?id=$1 [L]
RewriteRule ^movies/genre/([^/]*)/$ /moviescat.php?id=$1 [L]



RewriteRule ^edit/([^/]*)$ /edit.php?id=$1 [L]
RewriteRule ^edit/([^/]*)/$ /edit.php?id=$1 [L]
RewriteRule ^delete/([^/]*)$ /delete.php?id=$1 [L]
RewriteRule ^delete/([^/]*)/$ /delete.php?id=$1 [L]

RewriteRule ^user/([a-zA-Z0-9]+)/likes/$ /profile/likes.php/?user_username=$1 [L]
RewriteRule ^user/([a-zA-Z0-9]+)/likes$ /profile/likes.php/?user_username=$1 [L]


RewriteRule ^post/add/$ /post/add.php [L]
RewriteRule ^post/add$ /post/add.php [L] 

这里是 javascript

<script>
     $(document).ready(function(){
         $('#loader').on('inview', function(event, isInView) {
             if (isInView) {
                 var nextPage = parseInt($('#pageno').val())+1;
                 $.ajax({
                     type: 'POST',
                     url: '../../paginationmovies.php',
                     data: { pageno: nextPage },
                     success: function(data){
                         if(data != ''){                             
                             $('#response').append(data);
                             $('#pageno').val(nextPage);
                         } else {                                
                             $("#loader").hide();
                         }
                     }
                 });
             }
         });
     });
 </script>

有趣的是,一切都在http://localhost/movies/ 上运行 我希望你能理解并帮助我。

就像htaccess 忽略了这部分javascript:

url: '../../paginationmovies.php',

【问题讨论】:

  • 什么不能按预期工作?那个文件存在吗?使用浏览器的 DevTools/Network 选项卡查看是否发出了请求。该javascript文件位于哪里/在哪个文件夹中?
  • 我希望它像这样工作 - localhost/movies/genre/Action 或任何其他类型 /Adventure 等。但是 javascript 文件仅适用于 localhost/moviescat.php?id=Action 。 Javascript 位于 - localhost/template/pages/infinitescrollmovies.php 中,我在 localhost/moviescat.php 的代码中引入了它。是的,文件存在,相同的文件在 localhost/movies/index.phplocalhost/movies 上工作,javascript 加载并且工作得很好,但它似乎在这个链接 /movies/genre/Action 中被忽略了,但它在这个 /moviescat 中工作.php?id=动作
  • 您更改了主文档的 URL,因此这当然会影响任何相对 URL 的解析方式。最常用且最简单的解决方案:使用以/ 开头的路径来处理域根目录中的所有资源,这样主文档 URL 的路径级别深度就不会再影响这一点。
  • 不,网址是正确的。我敢肯定。无论如何我都试图改变它,但它无论如何都不起作用。这是屏幕截图-imgur.com/a/gCgMxlE 在第一个屏幕截图中,您可以看到加载程序消失了,一切正常,在第二个屏幕截图中没有任何效果,因为我使用 .htaccess 更改了 URL,在最后一个屏幕截图中,我'已经输入了 php 脚本的 URL,它在 .htaccess 中没有改变,一切正常并加载......
  • 以及为什么要投反对票,我花了一个多小时试图自己解决问题,然后又用谷歌搜索了一个小时,如果不是更多,也找不到问题,所以我只想问一些帮助或其他解决方案。我什至尝试将脚本放在同一个文件中的所有代码下面,但没有任何效果。

标签: javascript php mysql apache .htaccess


【解决方案1】:

感谢您的帮助和投票。然而,问题出在htaccess

从此改行:

RewriteRule ^movies/genre/([^/]*)$ /moviescat.php?id=$1 [L] RewriteRule ^movies/genre/([^/]*)/$ /moviescat.php?id=$1 [L]

到这里:

RewriteRule ^movies/genre/([a-zA-Z0-9]+)/$ /movies/genre/?id=$1 [L] RewriteRule ^movies/genre/([a-zA-Z0-9]+)$ /movies/genre/?id=$1 [L]

希望这对遇到相同/类似问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多