one Rewrite is messing up another?

Discuss practical ways rearrange URLs using mod_rewrite.

one Rewrite is messing up another?

Postby halfasleeps » Wed Jul 23, 2008 7:37 am

ok I have this rewrite rule:
rewritecond %{http_host} ^kranichs.com [nc]
rewriterule ^(.*)$ http://www.kranichs.com/$1 [r=301,nc]


which inforces always having http://www always prefiex my domain...it always has worked and still does.



I now added this:
RewriteRule ^.*_(\d+)c\.htm(&.*)? /K_category.php?cid=$1$2

which makes something like this:
http://www.kranichs.com/Simon_G_53c.htm
give you this page:
http://www.kranichs.com/K_category.php?cid=53

which works but if you dont type in the www. part and try to access it like this: http://kranichs.com/Simon_G_53c.htm it still takes you to the right page but it the URL actually changes to http://www.kranichs.com/K_category.php?cid=53 which I dont want the user to see this I want the user to always see the prettier URL.

I know this is caused because of the other rule that enforces the www. but I dont know how to fix this?

I hope this makes sense.

Can anyone help?

THANKS!
halfasleeps
 
Posts: 9
Joined: Wed Jul 23, 2008 7:36 am
Location: Altoona, PA. USA.

Postby richardk » Wed Jul 23, 2008 8:01 am

Both rules run in the same request. The www is added, the URL is changed to /K_category.php?cid=53, and then the visible redirect happens.

You need the L flag to stop processing when the rule has matched.
You should do the redirect first.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

RewriteRule ^.*_(\d+)c\.htm(&.*)?$ /K_category.php?cid=$1$2 [QSA,L]

Now, the www is added and the visible redirect happens. Then the URL is changed to /K_category.php?cid=53 without the user knowing.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby halfasleeps » Wed Jul 23, 2008 8:28 am

richardk wrote:Both rules run in the same request. The www is added, the URL is changed to /K_category.php?cid=53, and then the visible redirect happens.

You need the L flag to stop processing when the rule has matched.
You should do the redirect first.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

RewriteRule ^.*_(\d+)c\.htm(&.*)?$ /K_category.php?cid=$1$2 [QSA,L]

Now, the www is added and the visible redirect happens. Then the URL is changed to /K_category.php?cid=53 without the user knowing.




Thank you very much!!!! I simply added a [L] flag to each rule and it worked.

I think I understand that by putting the [L] it broke the operation in 2 to requests instead of one. The redirection happened in the first request and then the rewrite happend after the redirection in the second request. ....but I am still confused as to why it wasnt working in the first place when it was all being done in the same request. If someone wouldnt mind explaining this a little deeper I think it could help prevent me from running into this often in the future.

THANKS!

I am very new to apache/mod_rewrite/and regex. (I do know some OOP languages tho) Anyways I am trying to learn, I just read the book Apahe mod_rewrite by Rich Bowen which helped a lot.
halfasleeps
 
Posts: 9
Joined: Wed Jul 23, 2008 7:36 am
Location: Altoona, PA. USA.

Postby richardk » Thu Jul 24, 2008 1:37 pm

I am still confused as to why it wasnt working in the first place when it was all being done in the same request.

The redirection has to happen at the very end (it starts a new request), so if you have
Code: Select all
rewritecond %{http_host} ^kranichs.com [nc]
rewriterule ^(.*)$ http://www.kranichs.com/$1 [r=301,nc]

RewriteRule ^.*_(\d+)c\.htm(&.*)? /K_category.php?cid=$1$2

it knows it has to add www, but it doesn't redirect yet. Then the second rule changes the URL and finally the redirect happens.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 34 guests

cron