2016-12-05 85 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