Adding a trailing slash

Hey Guys,

I have my .htaccess file setup like a charm, but it shorts 2 basic principles.

My current rewriterule is sending everything to index.php with a front controller waiting to kick some ass :stuck_out_tongue:

I however want to eliminate the chance of users confusing the system and want to create an effective solution, so, what I want to happen is:

  1. Create a rewriterule (301) which will strip away any trailing extension (.php, .html, .htm) and redirect it to the appropriate filename so about.php will be about/ and products.php will be products/
  2. Not knowing who your target market is, I may end up having users add pages and forget to link them with a trailing /. so I would like to with my 2nd rewrite check if there is a trailing slash, and if not, add it. So effectively make domain.com/about = domain.com/about/
  3. My rewrite rule as I have it now will send (*.) to index.php

Now, I have the following rules “in place” but they’re not working like I would want.

What is important to remember here is that there will always be just 1 file and thats index.php, so, the only file which will exist is index.php, the rest although they might be called will highly unlikely ever exist.

For number 1 I have:
(This rule does not work at all :slight_smile: It rewrites the request (lets say about.php) to the root folder (so if the request was www.domain.com/site/about.php it will redirect to www.domain.com/about.php/ - It does add the trailing slash tho …)
I would like to make this generic so that if the site is located under a sub folder, it should direct to that SPECIFIC folder and not to the root. I have tried removing the / infront of the /$1/ but then it directs to www.domain.com/var/www/site/about.php (absolute path?)

Effectively this rule does not work :slight_smile:


RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} !index.php [OR]
RewriteCond %{REQUEST_URI} ^(.*).php [OR]
RewriteCond %{REQUEST_URI} ^(.*).htm[OR]
RewriteCond %{REQUEST_URI} ^(.*).html [OR]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301] 

For number 2 I have:

This rule is practically the same as above, but here I make the assumption that there is no extension and the filename is not index.php :slight_smile:


#RewriteCond %{REQUEST_FILENAME} !-f [OR]
#RewriteCond %{REQUEST_URI} !index.php [OR]
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteRule ^(.*)$ /$1/ [L,R=301] 

And lastly number 3 (which works, but any ideas, why not :wink: ):

The only one which works, but hell give your suggestions where needed :slight_smile:


# Rewrite all filenames to mod rewrite filenames
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]

In a nutshell, any url which is not in the /pagename/ format must be rewritten and sent to index.php :slight_smile:

Anyone?

Is this possible? Can someone just say is it possible to redirect a user to the same sub level folder inwhich the .htaccess file is located? PLEASE I am REALLY looking for an answer here? :frowning:

You are way over thinking 1 and 2. For both you only need one RewriteCond (and it’s already among the ones you have!) and one RewriteRule. Nothing more :slight_smile:
Let me know if you need more hints :slight_smile:

With regards to number 3, browse through the Apache Configuration forum a bit to see what dklynn and I have to say about :redhot: (.*) :redhot:
Also, drop the [NC] on that one.

:slight_smile:

Hey ScallioXTX,

Thanks for your reply.

I’m relooking the scenario, and although I am not quite sure what precisely I need to write, I can put in words the rewrite cond and rules you are saying here:

  1. rewritecond - Check if it is not a filename
  2. 301 redirect everything from request to /request/

The problem I have is using /request/ redirects all users to the base so in essence, if the project is located at domain.com/site/about.php it will most likely redirect to domain.com/about/ and not domain.com/site/about/

I will change the (.*) to your recommendation :wink:

If you put the .htaccess in the root it will indeed do that, but not if you put the .htaccess in the site directory.
The .htaccess works relative on the path it’s in :slight_smile:

Thank you for your reply.

I am really in tears, I really don’t know what else to do and I am honest I dont know how to fix this. Please please please help me. I rewrote the rules below but I am not getting the desired results.

The first rule is the redirect rule. The .htaccess file is located in the site folder, not root folder. I am trying to see if the request has an extension, rewrite it accordingely. Although this rule ALMOST works, here is the outcome:


RewriteCond %{REQUEST_URI} ^(.*)\\.([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)\\.(.*)$ $1/ [L,R=301]

  1. if i request something like domain.com/site/about.php it rewrites to domain.com/var/www/site/about/ so it automatically adds the /var/www which then returns nothing (file not found). It however does rewrite the about.php to about/
  2. if I however request something like domain.com/site/about/ it rewrites to domain.com/var/www/site/index/, disregarding about completely and rewrites to index/ - It still does not find the file required but the big issue here is that it redirects to index/ which it should not?

I am looking for the post you are referring to with the (.*) but cannot find it. Can you perhaps paste a link?


# Rewrite all filenames to mod rewrite filenames
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

I just tried the following locally and it works like a charm:


RewriteEngine On
RewriteBase /site/

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9_-]+)\\.(php|html?) $1/ [L,R=301]

Make sure to clear -or disable- the cache of your browser when you’re working with 301 redirect, since browsers are allowed to cache the redirect. Even better is to put R=302 in the rule while your testing (which may not be cached) and then change to R=301 once you’re sure it’s working.