1

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
Community
  • 1
  • 1
Geoff
  • 583
  • 2
  • 7
  • 25

1 Answers1

2

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Just done a more indepth test with this now I'm home, it's not working with the css files. I've removed the direct links on communications.php, so that they're back like and it's not working again. Should this work with these links like so? I don't see why not as it's in the same folder as index.php and that works fine. Have cleared browser cache btw. – Geoff Dec 05 '16 at 19:30
  • `RewriteRule ^(js|css)/ - [L,NC]` will skip any file inside `/js/` folder. Move this rule just below `RewriteEngine On` line and retry. – anubhava Dec 05 '16 at 19:50
  • Needing this to include images and documents now, so it ignores .png / .gif / .jpg / .docx / .xlsx etc. Any help much appreciated :) – Geoff Jan 20 '17 at 23:22
  • 1
    Change first rule to this: `RewriteRule ^(js|css)/|\.(png|gif|jpe?g|docx?|xlsx?)$ - [L,NC]` – anubhava Jan 21 '17 at 03:10