Solved

URL rewrite

Posted on 2006-12-01
15
266 Views
Last Modified: 2010-03-04
Trying it for the first time and having a problem with this stuff


RewriteEngine on

RewriteRule ^lotshop/index.php?main_page=product_info&cPath=2&products_id=([0-9]+)$ /trackdays/$1/ [R]
RewriteRule ^trackdays/([0-9]+)$ /trackdays/$1/ [R]
RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1

the problem lies with the first line. The second two work great.

Where have I gone wrong???


TIA
0
Comment
Question by:abenbow
  • 8
  • 7
15 Comments
 

Author Comment

by:abenbow
Comment Utility
hmm

it's going to be the & isn't it.
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
No, it's actually the entire query string.

RewriteRule does not see the query string at all, so you can't match it with a regex pattern.

Instead, you need to use a RewriteCond and test the %{QUERY_STRING} variable.
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
For instance, your first line could become:

RewriteCond %{QUERY_STRING} main_page=product_info
RewriteCond %{QUERY_STRING} cPath=28
RewriteCond %{QUERY_STRING} products_id=([0-9]+)
RewriteRule ^lotshop/index.php$ /trackdays/%1

The %1 matches "the grouped parts (parentheses!) of the pattern from the last matched RewriteCond directive in the current bunch of conditions" (from the Apache manual).
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
(Sorry, I forgot the R flag at the end of the RewriteRule.)
0
 

Author Comment

by:abenbow
Comment Utility
Ok thanks for that

this is where I am at now

RewriteEngine on

RewriteCond %{QUERY_STRING} main_page=product_info
RewriteCond %{QUERY_STRING} cPath=2
RewriteCond %{QUERY_STRING} products_id=([0-9]+)
RewriteRule ^lotshop/index.php$ /trackdays/%1/ [R]

RewriteRule ^trackdays/([0-9]+)$ /trackdays/$1/ [R]
RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1

basically it doesn't work. It takes an age to load the page - miniutes - and just display "page can not be found" The URL is not rewritten
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
That could be an infinite loop. Your second rule seems to be redirecting /trackdays/23 to /trackdays/23/, which the third rule will then redirect to /lotshop/index.php?main_page=product_info&cPath=28&products_id=23, which the first rule will then redirect to /trackdays/23/, and round and round it goes, forever.

Eventually Apache will realise it's not going anywhere and just timeout.

You need to redesign your rules so that they aren't cyclic.
0
 

Author Comment

by:abenbow
Comment Utility
hmm, ok

This is what I thought was happening

SAMPLE URL
http://www.lotus-on-track.com/lotshop/index.php?main_page=product_info&cPath=2&products_id=128

the sections that you have posted would turn that URL into

http://www.lotus-on-track.com/trackdays/128/

this


RewriteRule ^trackdays/([0-9]+)$ /trackdays/$1/ [R]

would check for the trailing / and rewrite the URL to include it if it's not there.

then this

RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1

would get the send the code the correct information (lotshop/index.php?main_page=product_info&cPath=2&products_id=128) while still allowing http://www.lotus-on-track.com/trackdays/128/ to be displayed in the address bar.

If I just use

RewriteEngine on

RewriteRule ^trackdays/([0-9]+)$ /trackdays/$1/ [R]
RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1

then putting http://www.lotus-on-track.com/trackdays/128 into the address bar converts it to http://www.lotus-on-track.com/trackdays/128/ and displays it correctly, leaving http://www.lotus-on-track.com/trackdays/128/ in the address bar.

So now I'm confused
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:abenbow
Comment Utility
OK

I have tried your code on it's own

RewriteEngine on

RewriteCond %{QUERY_STRING} main_page=product_info
RewriteCond %{QUERY_STRING} cPath=2
RewriteCond %{QUERY_STRING} products_id=([0-9]+)
RewriteRule ^lotshop/index.php$ /trackdays/%1/ [R]

and it's not working properly

Basically it converts

http://www.lotus-on-track.com/lotshop/index.php?main_page=product_info&cPath=2&products_id=128

to

http://www.lotus-on-track.com/trackdays/128/?main_page=product_info&cPath=2&products_id=128
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
Ah, sorry. (I just solved this very problem in another thread.)

To clear the query string, you just append a question mark to the end of the RewriteRule target:

RewriteRule ^lotshop/index.php$ /trackdays/%1/? [R]

I'm assured (by the poster of the other thread) that Apache is smart enough to see the empty query-string and it won't put the question mark on the end of the rewritten URL.
0
 

Author Comment

by:abenbow
Comment Utility
ok, thanks for that. It does fix the code you posted

any ideas on how do I stop it from looping??
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
Well, you need to decide what the underlying URL is (the actual path to an actual document or script) and what is the superficial URL is (the one the user sees in their browser window).

The superficial URL should internally (without the user noticing) redirect to the underlying URL.

I get the impression you want the URL the user sees to be /trackdays/23/ (for instance), and the actual script is found at /lotshop/index.php. But I might be wrong. Let me know how your site is (or is intended to be) structured with regards to this.
0
 

Author Comment

by:abenbow
Comment Utility
ok

this is the site
http://www.lotus-on-track.com/lotshop/

.htaccess is located here
http://www.lotus-on-track.com

Currently an event url looks like this
http://www.lotus-on-track.com/lotshop/index.php?main_page=product_info&cPath=2&products_id=160

I want to change that to
http://www.lotus-on-track.com/trackdays/160/

If I show the URL
http://www.lotus-on-track.com/trackdays/160/

then I need .htaccess to silently send
http://www.lotus-on-track.com/lotshop/index.php?main_page=product_info&cPath=2&products_id=160

to the code so that the code still works.

All the links in the code will sow up as
http://www.lotus-on-track.com/lotshop/index.php?main_page=product_info&cPath=2&products_id=160

so I need .htaccess to convert those to
http://www.lotus-on-track.com/trackdays/160/

in the address bar.





RewriteEngine on

RewriteCond %{QUERY_STRING} main_page=product_info
RewriteCond %{QUERY_STRING} cPath=2
RewriteCond %{QUERY_STRING} products_id=([0-9]+)
RewriteRule ^lotshop/index.php$ /trackdays/%1/? [R]


RewriteRule ^trackdays/([0-9]+)$ /trackdays/$1/ [R]
RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1


Your section will convert the URL to the format I want.
My section will get the right information to the code and display the correct event on the page whilest leaving the URL http://www.lotus-on-track.com/trackdays/160/ in the address bar.

The problem is they won't work together. If you put them together then you end up looping and ultimately the page doesn't load.


Thanks

Andy
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 500 total points
Comment Utility
Ah, I now see why you ended up with rules that resulted in an infinite loop.

I don't think it's possible to achieve the result you're trying to get. When a redirect occurs (internal or external), the rewritten URL starts a new request, and the RewriteRules will be evaluated from scratch using the new requested URL. Which means your rules will always lead to an infinite loop.

The only way I can think of to avoid this is to set a new part in the query string of the real URL. Like this:

RewriteRule ^trackdays/([0-9]+)/$ /lotshop/index.php?main_page=product_info&cPath=2&products_id=$1&done_redirect=1

Then add a new RewriteCond to the first rule:

RewriteCond %{QUERY_STRING} !done_redirect=1

which will only trigger if the done_redirect=1 part of the query string is not present.

This might work, but I strongly recommend you modify your code to produce links that use the new URL. For a start, you need to consider a common problem with using silent redirects. The web browser thinks it is in a directory called /trackdays/ but no such directory actually exsts. So requests for images using relative paths will fail. So you may need to change HTML in affected pages so that images and files use absolute paths (beginning with a forward slash).
0
 

Author Comment

by:abenbow
Comment Utility
oh that is genius!

it works amazingly well.

Thank you
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
Glad you got it working.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now