Forum Moderators: phranque

Message Too Old, No Replies

htaccess doesn't do anything

Rewrite rules not working

         

WebWalla

2:39 pm on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to rewrite the URLs of a well-known forum software, but nothing happens - the URLs aren't re-written at all. I suspect that it may be a problem with the host, but while I wait for their reply I wanted to check if this code is OK ...

<Files ~ "\.html$">
RewriteEngine on
RewriteRule ^/(.*)forum_([0-9]*)_([0-9]*).html /viewforum.php?f=$1&start=$2
RewriteRule ^/(.*)forum_([0-9]*).html /viewforum.php?f=$1
RewriteRule ^/(.*)topic_([0-9]*)_([0-9]*).html /viewtopic.php?t=$1&start=$2
RewriteRule ^/(.*)topic_([0-9]*).html /viewtopic.php?t=$1
RewriteRule ^/(.*)post_([0-9]*).html /viewtopic.php?p=$1
</Files>

thanks.

jdMorgan

4:41 pm on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your code is in .htaccess, then remove the leading slash from your patterns. In .htaccess, the path to the local directory (where this .htaccess code is located) is stripped. In this case, the local diredctory is "/" so that path element will not be present.

There is also the possibility that you'll need to add:


Options +FollowSymLinks

at the beginning of your code. Don't add it if it's not needed, since it will just waste CPU cycles if that option is already enabled.

There's really no need to enclose the mod_rewrite code in a <Files> container, since the rules all specify that they apply only to requests ending with ".html".

Also, be aware that by using the regular-expressions quantifier "*", your patterns will accept *blank* fields in those positions. In general, the use of "+" would be more appropriate.

Jim

jdMorgan

4:46 pm on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, let's escape the literal periods, end-anchor the patterns, and use an [L] flag to prevent wasted CPU cycles:

RewriteEngine on
RewriteRule ^(.+)forum_([0-9]+)_([0-9]*)\.html$ /viewforum.php?f=$1&start=$2 [L]
RewriteRule ^(.+)forum_([0-9]+)\.html$ /viewforum.php?f=$1 [L]
RewriteRule ^(.+)topic_([0-9]+)_([0-9]*)\.html$ /viewtopic.php?t=$1&start=$2 [L]
RewriteRule ^(.+)topic_([0-9]+)\.html$ /viewtopic.php?t=$1 [L]
RewriteRule ^(.+)post_([0-9]+)\.html$ /viewtopic.php?p=$1 [L]

Jim

WebWalla

7:19 am on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

Thanks a bunch for your input, but it still doesn't do anything and my host refuses to give support on .htaccess files. I suspect that the file isn't even being executed (interpreted?).

Is there anything I can put in there as a simple test to see if it's even being read?

Thanks.

jd01

8:14 am on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To test if your file is working, try:

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test\.html$ http://www.example.com [R,L]

Then type yoursite.com/test.html into your browser -- if you are redirected to your home page, mod_rewrite is loaded -- if you are not redirected or receive a server error, uncomment (remove the # from) the first line of the code -- if you are still not redirected, or receive a server error, then mod_rewrite is not loaded.

Justin

WebWalla

9:31 am on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks - the redirection does work, without commenting out the first line, so I wonder why the commands abaove don't? Any ideas?

wizzud

11:59 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Can you give an example of link that doesn't work?

WebWalla

12:25 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



http://www.example.com/forums/viewforum.php?f=2 isn't re-written to what I'd expect it to be (http://www.example.com/forums/forum_2.html)

wizzud

12:47 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



With the .htaccess file in the root folder the following ...

RewriteEngine on
RewriteRule ^([^/]+)/forum_([0-9]+)\.html$ /$1/viewforum.php?f=$2 [L]

will rewrite [yoursite.com...] to [yoursite.com...]

With the .htaccess file in the /forums folder the rule becomes ...

RewriteRule ^forum_([0-9]+)\.html$ /forums/viewforum.php?f=$2 [L]

to do the same thing.

WebWalla

1:02 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks but I still have problems. There has to be something so simple that I'm doing wrong. I now have this in the root and still does nothing ...

RewriteEngine on
RewriteRule ^([^/]+)/forum_([0-9]+)_([0-9]*)\.html$ /$1/viewforum.php?f=$2&start=$3 [L]
RewriteRule ^([^/]+)/forum_([0-9]+)\.html$ /$1/viewforum.php?f=$2 [L]
RewriteRule ^([^/]+)/topic_([0-9]+)_([0-9]*)\.html$ /$1/viewtopic.php?t=$2&start=$3 [L]
RewriteRule ^([^/]+)/topic_([0-9]+)\.html$ /$1/viewtopic.php?t=$2 [L]
RewriteRule ^([^/]+)/post_([0-9]+)\.html$ /$1/viewtopic.php?p=$2 [L]

wizzud

1:25 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



so if you enter [<yoursite>...] into the URL input box of your browser what do you get? ... page not found?

WebWalla

2:50 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, in fact the page loads, but I was hoping that when clicking on the link http://www.example.com/forums/viewforum.php?f=2, I would be redirected to http://www.example.com/forums/forum_2.html, but in fact the dynamic link is maintained in my browser.

I wanted the more "static" format for MSN, Yahoo who I believe are not as good as G for following dynamic links.

jd01

5:16 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're going the wrong way -- you will need to change the links on you site to 'point' to the static URL, then use mod_rewrite to serve the information from the dynamic URL to the new static location.

See the Library [webmasterworld.com] for a longer explaination.

Justin