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

时间:2016-12-05 14:35:09

标签: javascript jquery ajax .htaccess mod-rewrite

我刚刚实现了漂亮网址,取自这个问题: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]

但这在一些地方打破了我的网站。在不是index.php的网页上,我必须添加完整的链接,例如<script src="https://www.example.com/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代码在同一个文件中运行,我在网站上有一个图像滚动条仍在工作,并且网站上的所有导航都在工作,但不是这样的部分。这个AJAX在实现.htaccess之前完美地工作,以删除.php并添加尾随/,但现在它甚至没有进入显示警告框的阶段。这个函数是从一些javascript代码中调用的,在另一个仍然可以正常工作的文件中调用。

我的网站目录如下:

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

1 个答案:

答案 0 :(得分:1)

有这样的话:

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]

在测试此更改之前,请务必完全清除浏览器缓存。