2016-12-05 84 views
1

我刚刚实施了漂亮的URL,取自此问题:htaccess question修改.htaccess文件后,网站部分无法正常工作

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

有这样的:

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] 

测试这种变化之前,请务必彻底清除浏览器缓存。

+0

刚做了一个更深入的测试,现在我回家了,它不适用于css文件。我删除了communications.php上的直接链接,以便它们回到并且它不再工作。这应该与这样的链接工作吗?我不明白为什么不,因为它在index.php相同的文件夹中,并且工作正常。顺便清除了浏览器缓存。 – Geoff

+0

'RewriteRule ^(js | css)/ - [L,NC]'将跳过'/ js /'文件夹内的任何文件。将此规则移至“RewriteEngine On”行下方并重试。 – anubhava

+1

太好了,谢谢!像魅力一样工作。 – Geoff