views:

1143

answers:

3

Hello,

I'd like to work with pages without trailing slashes. So now I want my URL's with an trailing slash to redirect (using .htaccess) to the same URL without the trailing slash.

I got two .htaccess files:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule (.*)  public/$1
</IfModule>

And one in my public folder:

DirectoryIndex index.html index.php
Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f       
    RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>

I tried adding the following rule to the .htaccess file in the public folder:

RewriteRule (.*)/$ $1 [R,L]

But then: example.com/public/page/view/2/

Redirects to: example.com/D:/webserver/public/page/view/2

Which is obviously not what I want...

A: 

Have you tried adding:

RewriteBase /public

to the .htaccess file in the public folder, to get:

<IfModule mod_rewrite.c>
    RewriteEngine On            
    RewriteBase /public

    RewriteRule (.*)/$ $1 [R,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f         
    RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
Daniel James
Just tried, but it won't redirect.
Erik
Do you mean it redirects without this line, but doesn't redirect with it? Also, it's not clear from your question where you put your 'RewriteRule (.*)/$ $1 [R,L]' in the '.htaccess' file.
Daniel James
With the your code I get the following results:example.com/page/view/1 >> 404 errorexample.com/page/view/1/ >> Redirects to example.com/public/page/view/1 and than displays a 404 message
Erik
Tried "RewriteBase /"?
Philippe Gerber
Where's your index.php file?
Daniel James
+1  A: 

Why do you have several .htaccess files? Where are the one's redirecting to public/$1 stored? You may very well be facing overriding RewriteRule directives that complicate things for you.

Without knowing your setup it's fairly hard to say how you should be using rewrites - can you specify what things are ACTUALLY looking like?

Fake51
I've reduced it all to one .htaccess file.When debugging I realized that some browsers added the trailing slash automatically because of some history of visited pages.Clearing my internet history, adding a rewritecond and rewriterule did the trick.
Erik
+1  A: 

Here is a an example from Apache documentation which you can use to solve your issue:

Trailing Slash Problem

Description:

Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.

Solution:

The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!

So, to do this trick we write:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$ $1/ [R]

Source

JohnZ