1

I want my website to look, for example www.example.com/about/

However currently I have a htaccess code which removes the .php and .html extensions

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

The above code makes my url look like, for example www.example.com/about It does not have the ending forward slash (trailing slash) as I don't know the htaccess code.

Can someone please give the code to add the forward slash(trailing slash) at the end of the url as the codes I found in the many other post doesn't work for me.

Also please take note, for the htaccess code which I have above, I had to change my links without its extension. eg; a href="index" instead of a href="index.html"

So to summarize

1.) What is the code for htaccess to make my url end with a forward slash (trailing slash) without the extension. eg; www.example.com/about/

2.) Currently, my links are set without its extension to make my current htaccess code. (eg; a href="index" instead of a href="index.html") So do I have to change it back with its extension?

3.) I need a code that works for both php and html

4.) Should I change all the links that links back to my index page as the domain name so that when someone clicks the Home link from another page from my website, it will appear as , for example eg; wwww.example.com instead of www.example.com/index

Thank you so much for taking the time to read, hoping for an answer as well as an explanation to the code.

2 Answers 2

1

Insert this rule just below RewriteEngine On line to add a trailing slash:

Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
Sign up to request clarification or add additional context in comments.

5 Comments

Now this new code allows the links to work properly, but there is no trailing slash at the end of it.
Reverted my changes and modified rules again. You need to make sure that your href links are also absolute so it should be href="/about/index" with a slash to start.
Now this code is working! Its linking each page the right way! However the CSS/images/js and so on are not loading. I tried to make it abosulte path such as ../images/example.jpg , /images/example.jpg, ../../css/example.css but nothing seems to work. The code is perfect, linking just the way it should. Just that its this small issue of loading css/images/jpg and so on.
See another update however you must use absolute paths for css/images i.e. href="/images/example.jpg" no ../ in these paths.
Awesome!!!!!!! It works!!! Thank you so much for your patience and guided help!!!! So basically all I needed to do was add a / before any folders such as css/images/js. Thank you once again, much appreciated!!!
0

See also DirectorySlash Apache directive http://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash

Comments

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.