Link to home
Start Free TrialLog in
Avatar of spiritwithin
spiritwithin

asked on

Apache rewrite - automatically add trailing slash to URLs

Hello.

I am having problems configuring my virtual host to support the automatical attachment of a trailing slash at the end of the url.

My config looks like this:

<VirtualHost *:80>
ServerName wl
DocumentRoot C:/wl
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule !\.(gif|jpg|png|css)$ /index.php
  ErrorDocument 404 /err/error.404.php
</IfModule>
</VirtualHost>

How do i have to configure those directives to assure that the traling slash is added automatically?

I am using Apache 2.2, running on win32.

Kind regards,
Leonidas
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Do you want to add the trailing slash to every request?
Avatar of spiritwithin
spiritwithin

ASKER

To every request that does not end with either .php or .css or .jpg / .gif / .png
RewriteCond %{SCRIPT_FILENAME} !^.+\.(gif|jpg|png|css)
RewriteRule !^(.+)$ $1/

If this does not work, please post the relevent log entries here for troubleshooting.
That last line should have been:

RewriteRule ^(.+)$ $1/
I'm afraid it doesnt work like intended.

I get a 404 now on all calls. I think that's happening because my index.php isnt called anymore. Any call should point to /index.php while to any request except for images, etc. a trailing slash should be applied.

I hope i described this well.
You have not described this well.  I understand you want gif|jpg|png|css requests to fall through.  I understand you want a slash put on all other incoming requests.  What about index.php?  Do you want all requests to be forwarded to /index.php?
Give specific examples.  "If I browse to 'foo', then I want 'bar' to come up"
Alright. Sorry, i assumed this was clear due to initially pasting my virtualhost configuration.

In fact, i want:

1.) All requests to be passed to /index.php
while
2.) requests to gif, png, etc. to be ignored
and
3.) a trailing slash to be added to all requests that are not going to gif, png, etc.
Let me restate so I'm sure I have this right:

You want any requests for a gif, png, jpg, or css file to be ignored.
You want EVERY URL rewritten to /index.php, which is then suffixed with a trailing slash.  In other words, you want every URL rewritten to /index.php/

Correct. I can add more exception file types myself later, once i know how the RewriteRule looks like.

I am using clean URLs.. so when i call

/wl/something

I want it to become

/wl/something/

The term "index.php" should never be seen in any request, but all requests should internally point to /index.php
Try this:

RewriteCond %{SCRIPT_FILENAME} !^.+\.(gif|jpg|png|css)
RewriteRule ^(.+)$ /index.php/$1/
Works generally, but adds a only trailing slash only at the first level.

When i open

/wl

it becomes /wl/

But when i open

/wl/something

It stays like that, without a trailing slash
Please post the logs relevant to that rewrite attempt.
That's all there is to it:

127.0.0.1 - - [07/Jun/2007:01:11:35 +0200] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Jun/2007:01:11:39 +0200] "GET /something HTTP/1.1" 200 -
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin