修改.htaccess文件后,网站的某些部分无法正常工作

编程入门 行业动态 更新时间:2024-10-19 10:21:40
本文介绍了修改.htaccess文件后,网站的某些部分无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚实现了来自以下问题的漂亮URL: htaccess问题.

Options +SymLinksIfOwnerMatch RewriteEngine On RewriteBase / # Ensure www on all URLs. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ www.%{HTTP_HOST}/$1 [L,R=302] # Ensure we are using HTTPS version of the site. RewriteCond %{HTTPS} !on RewriteRule ^ %{HTTP_HOST}%{REQUEST_URI} [L,R=302] RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC] RewriteRule ^ %1/ [R=302,L] RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1/ [R=302,L] # Ensure all URLs have a trailing slash. RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302] # Remove all .php extensions without interfering with .js or .css. RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([^.]+?)/?$ $1.php [L]

但这在很多地方破坏了我的网站.在不是index.php的页面上,我不得不添加完整的链接,例如<script src="www.example/js/bootstrap.min.js"></script>而不是仅添加<script src="/js/bootstrap.min.js"></script>,这是否是对此的最佳解决方案?

使用我的AJAX代码

$.fn.login_user = function () { var email = $('#login_email').val(), password = $('#login_password').val(), remember_me = $('input:checkbox:checked').val(); alert("test: " + email + " " + password); $.ajax({ type: 'POST', url: '../php/login.php', data: {email: email, password: password, remember_me: remember_me}, success: function (response) { alert(response); } }); };

jQuery代码在同一文件中运行,我在网站上有一个图像滚动器仍在工作,并且网站上的所有导航都在工作,但没有像这样的部分.在实施.htaccess删除.php并添加尾随/之前,此AJAX可以完美地工作,但是现在它甚至还没有进入显示警告框的阶段.该功能是通过一些javascript代码调用的,而该文件仍在另一个文件中,该文件仍然运行正常.

我的网站目录如下:

mysite - index.php - communications.php /php/ all php files here /js/ all js and jquery files here /css/ all css files here

解决方案

像这样:

Options +FollowSymLinks RewriteEngine On # Ignore anything in js/css folders RewriteRule ^(js|css)/ - [L,NC] # Add www and turn on https in same rule RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ www.%1%{REQUEST_URI} [R=301,L,NE] # Adding a trailing slash RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{REQUEST_FILENAME} !-f RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301] # Remove index.php externally RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{THE_REQUEST} /index\.php [NC] RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC] RewriteRule ^ %1 [L,R=301,NE] # remove .php externally RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,NE,L] # Remove all .php extensions without interfering with .js or .css. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([^.]+?)/?$ $1.php [L]

在测试此更改之前,请确保完全清除浏览器缓存.

I've just implemented Pretty URL's, taken from this question: htaccess question.

Options +SymLinksIfOwnerMatch RewriteEngine On RewriteBase / # Ensure www on all URLs. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ www.%{HTTP_HOST}/$1 [L,R=302] # Ensure we are using HTTPS version of the site. RewriteCond %{HTTPS} !on RewriteRule ^ %{HTTP_HOST}%{REQUEST_URI} [L,R=302] RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC] RewriteRule ^ %1/ [R=302,L] RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1/ [R=302,L] # Ensure all URLs have a trailing slash. RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302] # Remove all .php extensions without interfering with .js or .css. RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([^.]+?)/?$ $1.php [L]

But this has broken my site in a few places. On pages that aren't index.php, I've had to add the complete link such as <script src="www.example/js/bootstrap.min.js"></script> instead of just <script src="/js/bootstrap.min.js"></script>, is this the best fix for that?

On my AJAX code

$.fn.login_user = function () { var email = $('#login_email').val(), password = $('#login_password').val(), remember_me = $('input:checkbox:checked').val(); alert("test: " + email + " " + password); $.ajax({ type: 'POST', url: '../php/login.php', data: {email: email, password: password, remember_me: remember_me}, success: function (response) { alert(response); } }); };

jQuery code is functioning from the same file, I've got an image scroller on the site that's still working, and all navigation on site is working but just not sections like this. This AJAX was working perfectly before implementing the .htaccess to remove .php and add the trailing /, but now it's not even getting to the stage of displaying the alert box. This function is called from some javascript code, in another file which is still working perfectly.

My site directory is like so:

mysite - index.php - communications.php /php/ all php files here /js/ all js and jquery files here /css/ all css files here

解决方案

Have it like this:

Options +FollowSymLinks RewriteEngine On # Ignore anything in js/css folders RewriteRule ^(js|css)/ - [L,NC] # Add www and turn on https in same rule RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ www.%1%{REQUEST_URI} [R=301,L,NE] # Adding a trailing slash RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{REQUEST_FILENAME} !-f RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301] # Remove index.php externally RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{THE_REQUEST} /index\.php [NC] RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC] RewriteRule ^ %1 [L,R=301,NE] # remove .php externally RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,NE,L] # Remove all .php extensions without interfering with .js or .css. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([^.]+?)/?$ $1.php [L]

Make sure to completely clear your browser cache before testing this change.

更多推荐

修改.htaccess文件后,网站的某些部分无法正常工作

本文发布于:2023-11-01 12:48:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   文件   工作   网站   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!