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

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

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?
you should use an absolute path for images and styles
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?
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: ljis17.com/honorary_list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.