problem with adding trailing slash

Using a single web hosting account to host multiple sites

problem with adding trailing slash

Postby rynox » Fri Aug 14, 2009 4:51 am

Hello everyone, im having a slight problem with mod rewriting.

I managed to let http://forum.domain.com/ to go to http://www.domain.com/forum/ ... but i want everything after http://forum.domain.com/ to go to a specific file and call a script.

An example would be:

http://forum.domain.com/test/ would to go www.domain.com/forum/?query=test .
My problem is when i go to http://forum.domain.com/test it sends me to http://forum.domain.coim/test/?query=test
( On a sidenote: http://forum.domain.com/test/test2/ should go (silently ofc) to www.domain.com/forum/?query=test/test2/ )

When i go to http://forum.domain.com/test/ the script works just fine.
This is why i'd like to add a trailing slash to the end but only if it doesnt end with a trailing slash allready.

So http://forum.domain.com/test should go to http://forum.domain.com/test/ before it gets send to www.domain.com/forum/?query=test (silent redirect)

Could anyone help me out? I'm using the following code:

Code: Select all
Options +FollowSymLinks +Indexes
#ErrorDocument 404 /notfound/index.php

RewriteEngine On

# Loop stopping code.
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^ - [L]



# Forward to root
RewriteCond %{HTTP_HOST} ^www\.domain.\com$ [NC]
RewriteRule ^(.*)$ /$1 [QSA,L]

#force www
RewriteCond %{HTTP_HOST} ^domain.\com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

# Display news /news/id/
#RewriteRule ^news/([0-9]+)/$ /news/viewnews.php?id=$1 [NC,L]

# Display an error /error/errortag/
#RewriteRule ^error/([a-z]+)/$ /errors/displayError.php?error=$1 [NC,L]

# Logout by visiting this link /logout/
#RewriteRule ^logout/$ /logout.php [NC,L]

# Forward to browse
RewriteCond %{HTTP_HOST} ^browse\.domain.\com$ [NC]
RewriteRule ^(.*)$ /browse/$1 [QSA,L]
# Forward to news
RewriteCond %{HTTP_HOST} ^news\.domain.\com$ [NC]
RewriteRule ^(.*)$ /news/$1 [QSA,L]
   # Forward to forums
   #RewriteCond %{HTTP_HOST} ^forum\.domain.\com$ [NC]
   #RewriteRule ^(.*)$ /forum/$1 [L]

   # Forward to forums
   RewriteCond %{HTTP_HOST} ^forum\.domain.\com$ [NC]
   RewriteRule ^(.*)$ /forum/index2.php?query=$1 [QSA,L]

# Forward to statistics
RewriteCond %{HTTP_HOST} ^stats\.domain.\com$ [NC]
RewriteRule ^(.*)$ /statistics/$1 [QSA,L]

# Forward to static
RewriteCond %{HTTP_HOST} ^static\.domain.\com$ [NC]
RewriteRule ^(.*)$ /static/$1 [QSA,L]

# Forward to account
RewriteCond %{HTTP_HOST} ^my\.domain.\com$ [NC]
RewriteRule ^(.*)$ /account/$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^my\.domain.\com/settings/$ [NC]
RewriteRule ^(.*)$ /account/settings/$1 [QSA,L]


# Forward every other subdomain to users
RewriteCond %{HTTP_HOST} ^(.+)\.domain.\com$ [NC]
RewriteRule ^(.*)$ /users/?name=%1 [L]


It's concerning the following chuck of code which is listed above:

Code: Select all
   # Forward to forums
   RewriteCond %{HTTP_HOST} ^forum\.domain.\com$ [NC]
   RewriteRule ^(.*)$ /forum/index2.php?query=$1 [QSA,L]
Last edited by rynox on Sat Aug 15, 2009 4:05 am, edited 3 times in total.
rynox
 
Posts: 4
Joined: Thu Jul 03, 2008 10:54 pm

Postby richardk » Fri Aug 14, 2009 12:49 pm

What happens if you go to forum.example.com with no mod_rewrite?
Where is the .htaccess file?

That mod_rewrite should not do that. Do you have some other mod_rewrite?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rynox » Sat Aug 15, 2009 3:17 am

Well actually theres way more mod rewrite to show you but at the time i thought it was irrelevant.

So at this moment it goes to the user's page. (myusername.domain.com displays the custom userpage of the registered user)

I've updated the code block in my first post of this topic.
rynox
 
Posts: 4
Joined: Thu Jul 03, 2008 10:54 pm

Postby richardk » Sun Aug 16, 2009 7:43 am

I don't see any reason why it would redirect forum.example.com/test/ to forum.example.com/test/?query=test.

Does /test/ exist?

Try
Code: Select all
Options +FollowSymLinks +Indexes

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^ - [L]

# Forward to forums
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^(.*)$ /forum/index2.php?query=$1 [QSA,L]

# Force www
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

# Forward to root
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ - [L]

# Add missing trailing slashes to browse, news, static
RewriteCond %{HTTP_HOST} ^(browse|news|static)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Forward to browse, news, static
RewriteCond %{HTTP_HOST} ^(browse|news|static)\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

# Add missing trailing slashes to statistics
RewriteCond %{HTTP_HOST} ^stats\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/statistics%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Forward to statistics
RewriteCond %{HTTP_HOST} ^stats\.example\.com$ [NC]
RewriteRule ^(.*)$ /statistics/$1 [QSA,L]

# Add missing trailing slashes to account
RewriteCond %{HTTP_HOST} ^my\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/account%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Forward to account
RewriteCond %{HTTP_HOST} ^my\.example\.com$ [NC]
RewriteRule ^(.*)$ /account/$1 [QSA,L]

# Forward every other subdomain to users
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /users/?name=%1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rynox » Mon Aug 17, 2009 2:58 pm

Thanks a lot richard!

I've got another question to be honest though.

Could you tell why i can't add another mod rewrite code chunk to let

http://forum.example.com/tag1/tag2/view/id/ go to a different php script?
I'm using the following code:

Code: Select all
# Forward to viewing a topic in a specific category (tags)
RewriteCond %{HTTP_HOST} ^forum\.example\.com/([a-z09]+)/view/([0-9]+)/$ [NC]
RewriteRule ^(.*)$ /forum/viewtopic.php?path=%1&viewid=%2 [QSA,L]

# Forward to forums
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^(.*)$ /forum/index.php?query=$1 [QSA,L]


This chunk of code always sends me to the second rewrite statement for some reason (its like i never added the first mod rewritecondition and rule)

http://forum.example.com/tag1/tag2/view/100001/ should go to www.example.com/forum/viewtopic.php?pat ... wid=100001 .

Could you tell me what im doing wrong here?

Thanks for your help :)
rynox
 
Posts: 4
Joined: Thu Jul 03, 2008 10:54 pm

Postby richardk » Tue Aug 18, 2009 3:51 pm

HTTP_HOST is the domain only (forum.example.com), use the RewriteRule to match paths.
Code: Select all
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^([a-z09]+)/view/([0-9]+)/?$ /forum/viewtopic.php?path=$1&viewid=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 28 guests

cron