Web Hosting Talk







View Full Version : problem with .htaccess - mod_rewrite


quanghuy
03-29-2005, 09:59 AM
I'm currently coding a news site. And I want to rewrite its URL like this:


RewriteEngine On
RewriteRule ^(.*)/$ news_browse_rcat.php?rcat=$1
RewriteRule ^(.*)/(.*)/$ news_browse_cat.php?rcat=$1&cat=$2
RewriteRule ^(.*)/(.*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA]


But, there are still some url that mustn't be rewritten (for example: site.com/about, site.com/contact,...), so I replace last one to:


RewriteEngine On
RewriteRule ^about$ about [QSA]
RewriteRule ^contact$ contact [QSA]
RewriteRule ^(.*)/$ news_browse_rcat.php?rcat=$1
RewriteRule ^(.*)/(.*)/$ news_browse_cat.php?rcat=$1&cat=$2
RewriteRule ^(.*)/(.*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA]


But.. it's still not working, it still jump to news_browse_rcat.php?rcat=about and news_browse_rcat.php?rcat=contact


Anyone can help me fix this problem ?

thanks in advanced :)

quanghuy
03-31-2005, 11:18 PM
noone can help me in this problem ?

joyfield
04-03-2005, 02:07 PM
Try with putting a [L] meaning "last" in the abaout and contact rule row so it will terminate there and not going on further.

quanghuy
04-05-2005, 04:56 AM
still can't solve this :(

RobLePom
04-05-2005, 07:47 AM
Have you tried using RewriteCond to set the condition when the RewriteRules should be used?

You can control it down to which script file is actually called (which sounds like what you want to do)

http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html#rewritecond

quanghuy
04-06-2005, 11:45 PM
i have read that article, but, u know, i'm new at this section. so can u please tell me what i will edit my code. thx in advanced

quanghuy
04-08-2005, 08:29 AM
come on, plz, i really need this :(

maxymizer
04-08-2005, 01:35 PM
When using .htaccess you must add RewriteBase directive, otherwise you won't get any matches.

RewriteEngine On
RewriteBase /path/to/your/dir/
RewriteRule ^(.*)/$ news_browse_rcat.php?rcat=$1
RewriteRule ^(.*)/(.*)/$ news_browse_cat.php?rcat=$1&cat=$2
RewriteRule ^(.*)/(.*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA]

saghir69
04-09-2005, 02:05 PM
quanghuy please post the original urls that you are trying to rewrite

especial the original url for
about
and
contact

i've just sorted similar thing for my site, so i might be able to help

seiken
04-10-2005, 04:13 AM
You can try the following

RewriteEngine On
RewriteRule ^about$ - [L]
RewriteRule ^contact$ - [L]
RewriteRule ^([^/]*)/$ news_browse_rcat.php?rcat=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ news_browse_cat.php?rcat=$1&cat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA,L]

seiken
04-10-2005, 05:09 AM
You can change the rewrite rules for about and contact to the following as I suspect that you are either being redirected to about/ and contact/, or the server because of multiviews or similar directive may be going for about.php and contact.php if those files are on your server.


RewriteRule ^about(/|\.php)?$ - [L]
RewriteRule ^contact(/|\.php)?$ - [L]

quanghuy
05-05-2005, 12:37 AM
Originally posted by seiken
You can try the following

RewriteEngine On
RewriteRule ^about$ - [L]
RewriteRule ^contact$ - [L]
RewriteRule ^([^/]*)/$ news_browse_rcat.php?rcat=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ news_browse_cat.php?rcat=$1&cat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA,L]


I have tried this, but sorry still not work :(

quanghuy
05-05-2005, 12:48 AM
Originally posted by saghir69
quanghuy please post the original urls that you are trying to rewrite

especial the original url for
about
and
contact

i've just sorted similar thing for my site, so i might be able to help

root
/about directory
/home
.htaccess
.3 php files below

that's all.

plz, i really need this

seiken
05-05-2005, 01:33 PM
Post any problems you have when you use the following. I have included the change that I suggested in the post following my first. So copy and paste this.

This should be in a ".htaccess" file in the root folder

RewriteEngine On
RewriteRule ^about(/|\.php)?$ - [L]
RewriteRule ^contact(/|\.php)?$ - [L]
RewriteRule ^([^/]*)/$ news_browse_rcat.php?rcat=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ news_browse_cat.php?rcat=$1&cat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA,L]


Also, remove the QSA if you don't need it or are not sure what it's for. I include it here because you have it in your original post.

=-D
05-06-2005, 12:51 AM
mate, what you should do is look at the error_log and see what the error is, that will help you

quanghuy
05-06-2005, 12:59 AM
Originally posted by seiken
Post any problems you have when you use the following. I have included the change that I suggested in the post following my first. So copy and paste this.

This should be in a ".htaccess" file in the root folder

RewriteEngine On
RewriteRule ^about(/|\.php)?$ - [L]
RewriteRule ^contact(/|\.php)?$ - [L]
RewriteRule ^([^/]*)/$ news_browse_rcat.php?rcat=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ news_browse_cat.php?rcat=$1&cat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([0-9]+)\.htm/$ news_read.php?rcat=$1&cat=$2&id=$3 [QSA,L]


Also, remove the QSA if you don't need it or are not sure what it's for. I include it here because you have it in your original post.

thanks lots, i've solved this :)

btw, why u all put "/" at the end of the url ( www.site.com/about/ ), how about when ppl type www.site.com/about ?

seiken
05-06-2005, 04:45 PM
RewriteRule ^about(/|\.php)?$ - [L]


The regex will match "about" "about/" and "about.php" and will stop any rewriting of those.

For eg if someone enters "http://server.com/about" the rewrite rule will match "about" and not modify it (-).

What happens then is the server will see that there is actually a directory named about/ and REDIRECT the webrowser to "http://server.com/about/". This happens transparently.

After which when the browser requests "http://server.com/about/" the rewrite rule will match "about/" and again not modify it. At which point the index for the directory should be served.

You can of course change the rewrite rule to this as the earlier version took a number of different urls (about about.php about/) into consideration because I wasn't sure what the request was suppose to be going for.


RewriteRule ^about(/|$) - [L]


This will stop any rewriting of pages being requested in the about/ directory.
There's really no need to match "http://server.com/about" because the other rewrite rules you have won't rewrite any request that doesn't end in "/".

EDIT: It is however safer to take the possibility into account which I've rewritten the rule to do.

quanghuy
05-09-2005, 09:37 AM
Man :)
( I put .htaccess in the "main" dir on server)
using ur code, when i type www.site.com/main/aaa/ , it will redirect to main/news_browse_rcat.php?rcat=aaa, right ? but when i typed www.site.com/aaa, this is the error msg i got:

Not Found
The requested URL /main/aaa was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

quanghuy
05-11-2005, 09:26 AM
then i type: www.site.com/main/aaa/aaa/1.htm got the same hero, type www.site.com/main/aaa/aaa/1.htm/ it's work

quanghuy
05-11-2005, 09:27 AM
so how to fix it ?