[SOLVED] Problem with mod_rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

[SOLVED] Problem with mod_rewrite

Post by fastfingertips »

Hello

I have some url's that look like:
http://localhost/wap/games/
http://localhost/wap/games/game_name/

This urls are rewrited to:
RewriteRule ^(.*)/(.+[^/])/$ index.php?module=$1&category=$2 [L]
RewriteRule ^(.+[^/])/$ index.php?page=$1 [L]

My problem is that if an user inserts only http://localhost/wap/games (without that ending /) the apache is trying to look in a directory that not exists or file that not exists. How can i add this (/) at the end of an url if not exists?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think you can remove it from the rewrite rule (or write your regular expression so that way the URL can have it or not and still get the same result). Just a thought.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I found the solution so if anyone elese needs it:

Code: Select all

RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.+) $1/ [R=301,L]
This will add a trailing slash to URL no matter it's length etc.
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post by tasteslikepurple »

this is the way i do it:

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
the top line makes sure that it's not a file, obviously you don't want /images/nav.gif to change to /images/nav.gif/
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Indeed you were right :), i'm saying this because i met the problem and i was thinking to post here :).

This problem has been solved.
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Post by asgerhallas »

Can I open this again?

I've been looking for exactly that solution, but now as I want to implement it, I can't make it work.

My setup is a little different, than the above. I have a proxy-server (apache 2.0), that uses ProxyPass to redirect to another apache server (ver 1.3) on the same machine port 8080. I want the trailing slashes to be added automatically as well, but it doesn't work, and I really can't tell why. Mod_rewrite is installed correctly. My vhosts looks like this:

Code: Select all

<VirtualHost *:8080>
SSLDisable
DocumentRoot /web/www.test.dk/www
ServerName www.test.dk
ServerAlias test.dk
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ /$1/ [L,R=301] 
DirectoryIndex index.html index.htm index.php index.php3 index.phtml
<Directory "/web/www.test.dk/">
Options +IncludesNOEXEC
</Directory>

Code: Select all

<VirtualHost xxx.xxx.xxx.xxx>
ServerName www.test.dk
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
I've been trying fx with http://www.test.dk/tester, but it tells me there a DNS error. Except from this everything works fine.

Does anybody have a clue??
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Post by asgerhallas »

UPDATE: Sometimes (i can't see the pattern) it show the following address after redirect: http://www.test.dk:8080//tester/
Post Reply