Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 11:58 PM   PM User | #1
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
Help with adding trailing slash to URL (.htaccess)

So I have been able to hide the .html extension on my pages, but I can't seem to get the trailing slash working. I've tried multiple codes with no luck. Here is how I configured my .html extension removal in .htaccess:

#force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# remove .html ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.html\sHTTP/1)
RewriteRule ^(.+)\.html$ /$1 [R=301,L,QSA]

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# rewrite to FILENAME.html if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L,QSA]
MBI is offline   Reply With Quote
Old 10-27-2012, 04:14 PM   PM User | #2
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 449
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Code:
 
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
replace .php with the extension you want to hide

Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.example.com/yourdirectory/$1/ [R=301,L]
add trailing slash
you will need to reference your target directory for this to work properly
stevenmw is offline   Reply With Quote
Old 10-30-2012, 06:01 AM   PM User | #3
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
I tried using the above codes, No change happened... Most of my website is in the same root directory, except for my blog so for the last line of your code I just tried:

Quote:
RewriteRule (.*)$ http://www.example.com/$1/ [R=301,L]


What am I missing? I also changed the .php to .html like you said
MBI is offline   Reply With Quote
Reply

Bookmarks

Tags
.htaccess, apache, htaccess, html

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:12 AM.


Advertisement
Log in to turn off these ads.