Remove trailing slash

New to mod_rewrite? This is a good place to start.

Remove trailing slash

Postby davidn » Tue Apr 21, 2009 4:56 am

Hi,

Just a small question. I'm trying to do it the other way around to this thread.

My site structure that handles url's like:

www.website.com/page/

I would very much like to force the last forward slash when somebody uses the link like:

www.website.com/page

So far I had no luck finding a solution :-(
davidn
 
Posts: 1
Joined: Tue Apr 21, 2009 4:48 am

Postby richardk » Tue Apr 21, 2009 9:48 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby javoec » Mon May 18, 2009 7:43 pm

richardk wrote:Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]


Thanks a lot for this richardk :D, is working great when I use 2 or more variables: http://example.com/page/subpage/ goes to http://example.com/page/subpage

The problem is that I'm using 4 simple rewrites:

Code: Select all
Options +FollowSymlinks
RewriteEngine on

### force NO slash at the end ###
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301]

### 1 variable ###
RewriteRule ^([a-z0-9_-]+)$ /?p=$1 [L]

### 2 variables ###
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)$ /?p=$1&q=$2 [L]

### 3 variables ###
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)$ /?p=$1&q=$2&r=$3 [L]

### 4 variables ###
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)$ /?p=$1&q=$2&r=$3&s=$4 [L]


Works great when I have 2, 3 or 4 variables, but the my problem lies when I have just 1 variable:

If I go to http://example.com/a_fake_folder/ the page is redirected to http://example.com/a_fake_folder and I get the variable p=a_fake_folder ..everything ok

If I go to http://example.com/a_real_folder/ the page goes to the <a_real_folder> folder in the root of my website, and not to http://example.com/a_real_folder to get the variable p=a_real_folder
If I deactivate the option to show the content of my folders, it goes to a 403 forbidden page.

I don't know if one of my rules is wrong, or if the rule that richardk wrote only works if the folder doesn't really exists.

Any comments or help is really appreciated.
javoec
 
Posts: 3
Joined: Mon May 18, 2009 7:31 pm
Location: Quito, Ecuador

Postby richardk » Tue May 19, 2009 3:58 pm

Remove
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-d

It means process the rule if the request (%{SCRIPT_FILENAME}) is not (!) for an existing directory (-d).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby javoec » Tue May 19, 2009 7:47 pm

richardk wrote:Remove
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-d

It means process the rule if the request (%{SCRIPT_FILENAME}) is not (!) for an existing directory (-d).


Thanks Richard! Kudos for the nice work.
I was searching the meaning of those conditions and I was trying the same. it works!.

Now, with that I can redirect http://example.com/news/ to http://example.com/news (knowing that <news> is an existing directory in my root folder)

But, what if I do have a subsite that is located in http://example.com/subsite/ (where the <subsite> folder exists)
Because by now the rule that forces not to use the last slash (/) will point to http://example.com/subsite and I will get the p=subsite variable, but that's not my goal. My goal is to enter the real http://example.com/subsite/ that has his own index.php

As a comment, I don't know if is better to use always a final slash (/) or not (like over here). I've read that is better to have URLs that end with a slash (/) because is faster for the server (or something like that).. but I thought that was only when I was reaching real folders (like the subsite example)

I didn't find a thread of final slash vs. no final slash in URLs, that could make me choose one of the 2 options.. since I only found this one about removing the final slash, I'm trying to do that. I could try the other option, but I don't know which one is better.. or if it doesn't matter.


Thanks again for the answer, any thoughts are always welcome.
BTW, this is a neat forum for mod_rewrite!!
javoec
 
Posts: 3
Joined: Mon May 18, 2009 7:31 pm
Location: Quito, Ecuador

Postby richardk » Wed May 20, 2009 8:35 am

But, what if I do have a subsite that is located in http://example.com/subsite/ (where the <subsite> folder exists)
Because by now the rule that forces not to use the last slash (/) will point to http://example.com/subsite and I will get the p=subsite variable, but that's not my goal. My goal is to enter the real http://example.com/subsite/ that has his own index.php

You could make /subsite an exception by adding
Code: Select all
RewriteRule ^subsite(/.*)?$ - [L]

after
Code: Select all
RewriteEngine On


Or you could make directories that contain an index.php file an exception
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
RewriteRule ^(.*[^/])/?$ - [L]

instead.

As a comment, I don't know if is better to use always a final slash (/) or not (like over here).

I doubt it would make any significant difference.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby javoec » Wed May 20, 2009 11:33 am

richardk wrote:You could make /subsite an exception by adding
Code: Select all
RewriteRule ^subsite(/.*)?$ - [L]

after
Code: Select all
RewriteEngine On



Wow, thanks! I saw that "-" on the right side of the equation means No Rewrite according to the Redirection with Mod_Rewrite thread (tutorial).. but I didn't realize how to use that...

richardk wrote:Or you could make directories that contain an index.php file an exception
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
RewriteRule ^(.*[^/])/?$ - [L]

instead.


Interesting, this one is a little more flexible. I didn't find a lot of info about the RewriteCond (Conditions) of mod_rewrite

richardk wrote:
As a comment, I don't know if is better to use always a final slash (/) or not (like over here).

I doubt it would make any significant difference.


I also believe that.. what matters is that the URL remains always the same... that or if the URL has to change, always use Redirect 301.


Thanks a lot for all the help Richard, this has been great in order to understand mod_rewrite. Kudos!
javoec
 
Posts: 3
Joined: Mon May 18, 2009 7:31 pm
Location: Quito, Ecuador

Postby richardk » Thu May 21, 2009 6:49 am

Interesting, this one is a little more flexible. I didn't find a lot of info about the RewriteCond (Conditions) of mod_rewrite

RewriteCond Directive.

Code: Select all
%{DOCUMENT_ROOT}/$1/index.php

%{DOCUMENT_ROOT} is a variable for your document root.
$1 is what is matched by the RewriteRule.
So if you request /dir or /dir/ it would produce /your/document/root/dir/index.php.

The -f CondPattern checks if the file exists.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond wrote:'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 109 guests

cron