why is this mod-rewrite not working

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
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

why is this mod-rewrite not working

Post by louie35 »

This code in the .htaccess file

Code: Select all

Options +FollowSymLinks
RewriteEngine on
RewriteRule cat/c/(.*)/km/(.*)/ cat.php?c=$1&km=$2
RewriteRule cat/c/(.*)/km/(.*) cat.php?c=$1&km=$2
is supposed to redirect to

Code: Select all

http://www.domain.tld/cat.php?c=Car_Security&km=111561
but it doesn't. It goes in a loop
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: why is this mod-rewrite not working

Post by RobertGonzalez »

Code: Select all

RewriteEngine on
RewriteRule cat/c/(.*)/km/(.*)/ cat.php?c=$1&km=$2
RewriteRule cat/c/(.*)/km/(.*) cat.php?c=$1&km=$2
Theoretically, the above should take this URL:

Code: Select all

http://www.domain.tld/cat/c/Car_Security/km/111561
or this URL:

Code: Select all

http://www.domain.tld/cat/c/Car_Security/km/111561/
and point that URL to

Code: Select all

http://www.domain.tld/cat.php?c=Car_Security&km=111561
That is not happening? The .*, as I understand it, means '0 or more of any single character'
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

yes indeed. It doesn't work. It gives the 404 page. I have other RewriteRules also for that page below this code but I added the [L] at the end and still doesn't do it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're missing the start (^) and end ($) character. Sorry, forgot about that totally.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

I have added the ^ and $ as well but still get the 404 page.
I'll have a look again into that today and let you know.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post your new rules so we can see them.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

this is the new rule I have

Code: Select all

#cat.php page
RewriteRule ^cat/c/([^/]*)/km/([^/]*)/$ cat.php?c=$1&km=$2 [L]
RewriteRule ^cat/c/([^/]*)/km/([^/]*)$ cat.php?c=$1&km=$2 [L]

#below that there is an old rule that i need it to saty there as well

# 1 key=value pairs, eg. 
# cat.php/km/123/ --> cat.php?product=123 
RewriteRule ^(cat|reviews_seller|search|carsales|carsales_extras|carsales_dealers|car_sale_news)\.htm/([^/]+)/([^/]*)/?$ $1.php?$2=$3 [L]

# 2 key=value pairs, eg.
RewriteRule ^(cat|products_list|rating|products_details|search|carsales|carsales_dealers)\.htm/([^/]+)/([^/]*)/([^/]+)/([^/]*)/?$ $1.php?$2=$3&$4=$5 [L]
the link is created using php like this:

Code: Select all

http://www.domain.tld/cat/c/car_security/km/111561/

or

http://www.domain.tld/cat/c/car_security/km/111561
I discovered that the reason i get sent back to the home page is because on the cat.php pahe a have some code as below:

Code: Select all

if($km == "" || !isset($_GET['km'])){
	ewd_db_close($conn);//close conn
	header ('Location: '.WEB_HOST.'index.php');
                exit();
}
i commented out that code so I rule it out and now the page displayes without results as the $km value is = ""

Can not understand why.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try something like this. I added the ? character before the closing regular expression delimiter and added a slash to the rewrite URL.

Code: Select all

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^cat/c/([a-zA-Z0-9-_]+)/km/([a-zA-Z0-9-_]+)/?$ /cat.php?c=$1&km=$2 [QSA, L] 
</IfModule>
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

That gives me a server error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.


Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


--------------------------------------------------------------------------------

Apache/1.3.37 Server at www. domain . tld Port 80
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'm not sure dude. Maybe look at the mod_rewrite manual for Apache 1.3 to see if it should be done differently than in Apache 2, which is what mine is.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

I can not undestand why the above rule gives this error with just going to the hme page not cat.php
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

why this works:

Code: Select all

RewriteRule ^(cat|products_list|rating|products_details|search|carsales|carsales_dealers)\.htm/([^/]+)/([^/]*)/([^/]+)/([^/]*)/?$ $1.php?$2=$3&$4=$5 [L]
and this doesn't

Code: Select all

RewriteRule ^cat/c/([a-zA-Z0-9-_]+)/km/([a-zA-Z0-9-_]+)/?$ /cat.php?c=$1&km=$2 [QSA, L]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I've given up with .htaccess based clean url's. I've started to send anything and everything (save certain things) to index.php and just deal with the parameters in php myself...

Code: Select all

#Options -Indexes -MultiViews #this may or may not be needed...
RewriteEngine on
RewriteCond $1 !^(index\.php|img|robots\.txt|css) # match on anything except whats in the brackets
RewriteRule ^(.*)$ index.php/$1 [L] #and send it all to index.php
Post Reply