修改.htaccess文件后,网站的某些部分无法正常工作 [英] Sections of site not working after modifying .htaccess file

查看:127
本文介绍了修改.htaccess文件后,网站的某些部分无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚实现了来自以下问题的漂亮URL:而不是仅添加<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.com - 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 ^ https://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 ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{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="https://www.example.com/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.com - 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 ^ https://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文件后,网站的某些部分无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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