Page 1 of 1

mod_rewrite problems

Posted: 2011/08/24 21:50:43
by TheSockMonster
Hi

I have spend the last couple of days scouring the internet but I can't figure out how to do this.

Basically I need the page...

www.domain.com/page1/12345678

to redirect to

www.domain.com/page2/page1/12345678

I thought I might be able to do it with mod_alias but I end up in an infinite loop.

What I have so far is

RewriteCond %{QUERY_STRING} ^(.*&)/page1/([0-9]+)?$ [NC]
RewriteRule ^/page1/([0-9]+)?$ /page2/page1/$2 [R=301,L]

but I'm just not hitting the rule.

I have spend hours on this and I just can't work out what I am doing wrong.

Any help would be very much appreciated.

Thanks

Si

mod_rewrite problems

Posted: 2011/08/29 18:18:07
by logan
Do you have some existing rewrite rules that would let you match on QUERY_STRING? Otherwise I'm not sure how that would work.

I would think you'd want something more like this:

[code]
RewriteBase /
RewriteRule ^page1/([0-9]+)$ /page2/page1/$1 [R=301,L]
[/code]

Shouldn't need the RewriteCond since it 's just duplicating what's happening in the RewriteRule.

Re: mod_rewrite problems

Posted: 2011/09/15 23:30:38
by gosteen
I'm also having problems with mod_rewrite. I am brand new to the LAMP world and just starting out ,
so please bear with me. I'm working through a book called "Professional LAMP: Linux®, Apache, MySQL®, and PHP5 Web Development".
I'm having trouble with the use of mod_rewrite I have the following php page:



This page works fine. I then modified the httpd.conf file. The original entry looked like


Options FollowSymLinks
AllowOverride None



I then modified it to look like


Options FollowSymLinks
AllowOverride None
RewriteEngine On
RewriteBase /
# Rewrite /category/item/ to catalog.php
RewriteRule ^(\w+)/(\w+)/?$ catalog.php?cat=$1&item=$2 [R,NC,L]




I then stopped and started the httpd service.

In the book (if I'm reading it right) says that now
http://localhost/catalog.php?cat=categoryvalue&item=itemvalue
should be equivalent to
http://localhost/categoryvalue/itemvalue

Any help with this would be greatly appreciated.

Re: mod_rewrite problems

Posted: 2011/09/16 00:08:47
by TrevorH
Here's how you debug something like this :-)

[code]
vi /etc/httpd/conf/httpd.conf
ReWriteLog "/var/log/httpd/rewrite_log"
ReWriteLogLevel 3
[/code]

That shows your pattern is wrong - very close but not quite right.

[code]
ReWriteEngine On
ReWriteBase /
ReWriteRule ^(\w+)/(\w+)$ catalog.php?cat=$1&item=$2 [R,NC,L]
[/code]

Re: mod_rewrite problems

Posted: 2011/09/17 02:38:44
by gosteen
Trevor,

I'm still swinging but have not hit the ball yet.

Here is the log info I entered into httpd.conf

[b]
#
# Let's make a rewrite log
ReWriteLog /var/log/httpd/rewrite_log
ReWriteLogLevel 3
#
#
[/b]

And here is my Rewrite rule. I'm including the whole tag in case there is something else I'm doing wrong
[b]


Options +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
RewriteEngine On
RewriteBase /
# Rewrite /category/item/ to catalog.php
RewriteRule ^(\w+)/(\w+)$ catalog.php?cat=$1&item=$2 [R,NC,L]


[/b]


The client machine I'm using has an ip address of 192.168.1.108 and the server box is 192.169.1.133

Here is the output of the rewrite log. I'v split the lines for easier reading. I'v been trying to find info on how to understand the log but I'm still pretty unclear about what is happening. Perhaps you can point me to some good information. I will keep digging.

[b]

192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (3)
[perdir /] add path info postfix: /var/www/html/cat -> /var/www/html/cat/item


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (3)
[perdir /] strip per-dir prefix: /var/www/html/cat/item -> var/www/html/cat/item


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (3)
[perdir /] applying pattern '^(\w+)/(\w+)$' to uri 'var/www/html/cat/item'


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (1)
[perdir /] pass through /var/www/html/cat


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (3)
[perdir /] strip per-dir prefix: /var/www/html/favicon.ico -> var/www/html/favicon.ico


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (3)
[perdir /] applying pattern '^(\w+)/(\w+)$' to uri 'var/www/html/favicon.ico'


192.168.1.108 - - [16/Sep/2011:06:14:30 --0400] [192.168.1.133/sid#88204b0][rid#8aa12f8/initial] (1)
[perdir /] pass through /var/www/html/favicon.ico

[/b]


As always thanks in advance for any help you may give

Re: mod_rewrite problems

Posted: 2011/09/17 11:18:12
by TrevorH
When I tried this, I put my rewrite rules inside the stanza.

Re: mod_rewrite problems

Posted: 2011/09/17 16:35:47
by gosteen
That did it!! Where do you want the beer sent? :lol: