0

Ok, I've fought with it for hours. I have 3 different .htaccess scripts which do what I need, but I'm unable to mix them together.

  1. Make a pretty url from (example.com/gallery.php -> example.com/gallery)

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ $1.php
    
  2. The script from #1 though forwards example.com/index.php to example.com/index, so this code removes index.php so example.com/index.php -> example.com

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ /%1 [R=301,L]
    
  3. The script should add a trailing slash so example.com/gallery -> example.com/gallery/

    # invoke rewrite engine
    RewriteEngine On
    RewriteBase /~new/
    
    # add trailing slash if missing
    rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
    

Can someone help me to combine those 3 scripts into one universal pretty URL scripts

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
yodalr
  • 9,778
  • 10
  • 32
  • 47

1 Answers1

1

add these directives to .htaccess in the root directory of your website

Options +FollowSymLinks
RewriteEngine On

# add trailing slash

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

# rewrite gallery/ to gallery.php

RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]

# redirect example.com/index.php to example.com/

RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • Thank you very much, this works alright, but it adds another problem. It won't load images and styles anymore, it goes to the page, but nothing is loaded, it shows just "empty pic thumbnails" and text from html, I think it's cause the page now thinks the images are in example.com/gallery/images/ while they really are in example.com/images/, any cure for that? – yodalr Apr 22 '13 at 19:09
  • you should use an absolute path for images and styles – Amine Hajyoussef Apr 22 '13 at 19:33
  • you mean like "example.com/images/image.jpg" instead of "images/image.jpg", I update the site locally, so it would cause little problems... any way around that? – yodalr Apr 22 '13 at 19:47
  • Anyways I think I got it working with base url, but it still doesn't load styles in IE, I'll figure it out eventually. Anyways tnx again Amine, I've also added you to my Honorary List on the website I'm building: http://ljis17.com/honorary_list/ – yodalr Apr 25 '13 at 23:00