Forum Moderators: phranque

Message Too Old, No Replies

Adding trailing slash with mod_rewrite

         

wplate

2:36 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



I found a mod_rewrite solution here...
[webmasterworld.com...]

So my .htaccess file looks like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!^[^.]*/$
RewriteRule (.+) /$1/ [R=301,L]

But when I try it, I watch my browser go through a process like this...

[domain.com...]
[domain.com...]
[domain.com...]
[domain.com...]
[domain.com...]

and so on until finally my browser gives up after many /s appear in the address.

It appears I have copied the the code correctly from the example jdMorgan provided. I don't understand regular expressions so there could be a problem I don't see.

Can anyone help? Thank you.

wplate

2:52 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



I knew if I finally broke down and posted my question I'd find the answer. Why does it always happen that way?

I found the solution here...
[webmasterworld.com...]

I changed my .htaccess file to use this instead

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$ 
RewriteRule ^(.*)$ /$1/ [R=301,L]

Now it works.

jdMorgan

2:55 pm on Aug 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wplate,

Welcome to WebmasterWorld!

There's a bug in that code. Try this instead:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(/$¦\.)
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]

Change the broken pipe "¦" character above to a solid pipe from your keyboard before use; Posting on this board modifies them.

Also, why does "index.php" suddenly appear in the URL? That is not a function of the code above, and should not be happening.

a regular expressions tutorial is available from a link in our forum charter, and a regex tutorial specific to mod_rewrite is also available in the library.

Jim

wplate

3:01 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Dang. My one test worked, but more general cases aren't working.

For example...
[domain.com...]
becomes
[domain.com...]

but then Safari complains...

[b]Safari can’t open the page.[/b]
Too many redirects occurred trying to open “http://domain.com/products/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

I guess I do need some .htaccess help afterall. Thank you.

wplate

3:06 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



thank you jdMorgan, for your reply. My last post crossed yours in the ether.

I made the change you suggested and also found another problem in my .htaccess that solved my last complaint.

I will verify your fix, thank you for taking the time.

wplate

3:18 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Also, why does "index.php" suddenly appear in the URL? That is not a function of the code above, and should not be happening.

I don't know the answer to this. I have some RedirectMatch 301 lines following the RewriteRule stuff (to catch old dead links), but none of them add index.php. I can only imagine that it is something that our web host does?

jdMorgan

5:15 pm on Aug 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possibly. I'd contact them and ask, because it is counter-productive to "expose" the name of your index file; It can lead to link-popularity and PR dilution.

The proper way to associate the file index.php with requests for the "/" local URL-path is to use the DirectoryIndex [httpd.apache.org] directive, which will not expose the filename. E.G.


DirectoryIndex index.php

You can add this directive yourself, just to see if it overrides something your host may be doing.

Jim