Forum Moderators: phranque

Message Too Old, No Replies

Problem accessing $ GET with rewritten URL

htaccess loop

         

micmania1

10:50 pm on Jun 14, 2008 (gmt 0)

10+ Year Member



I want my whole site to be rewritten from page.php to page/.

This part is working fine.

I can also use variables like page.php?var=1 as /page/var/1/

My problem is when I submit a form, the form will go to page.php?var=1

Now I think I need to redirect it to /page/var/1/ but when I do this, it is reading page.php?var=1 as it told leading the script back to square 1 resulting in a continuous never ending loop.

Is there a way around this?

Any help is appreciated,
Michael

jdMorgan

10:58 pm on Jun 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be a design problem, and a redirect is a very inefficient way of working around it, but to put this into context, you might consider posting the relevant code...

Jim

micmania1

12:09 am on Jun 15, 2008 (gmt 0)

10+ Year Member



Which relevant code would that be? I've tried so many things.

I've got a system which in theory should work, but sadly in reality it doesn't.

When a form is submitted it will be submitted using the get method the url will be as so: page/?var=1

I want it to change to /page/var/1/

Here is my code:
RewriteRule ^(.*)/\?(.*)=(.*)$ /$1/$2/$3/ [R=301]

RewriteRule ^(.*)/$ /hg/$1.php
RewriteRule ^(.*)/(.*)/(.*)$ /$1.php?$2=$3

jdMorgan

3:08 pm on Jun 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot change a URL using mod_rewrite. The URL is defined by the link on your page. So, first change the link on your page, and then use mod_rewrite to "associate" that link with the proper filepath on your server.

If you use a redirect, then the problem is that "it's too late," since the original URL has already been "published" on the page.

The normal procedure is:

  • link to /page/var/1/ (in this case, in your form)
  • internally rewrite requests for URL /page/var/1/ to filepath page.php?var=1
  • (optional) externally redirect any direct client requests (and only direct client requests) for page.php?var=1 to /page/var/1/ in order to more quickly "clean up" search engine listings.

    Your code may have been intended to do this. If it was, there are numerous problems with it, and all I can suggest is a re-write of the code:


    # Redirect direct client requests (only) for dynamic URL to static URL
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.]+\.php\?([^&=]+)=([^&\ ]+)\ HTTP/
    RewriteRule ^([^.]+)\.php$ http://www.example.com/$1/%1/%2/ [R=301,L]
    #
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L]
    RewriteRule ^([^/]+)/?$ /hg/$1.php [L]

    I replaced all occurrences of ".*" with more-specific negative-match patterns to greatly speed up matching and prevent unexpected/undesired results.

    More-detailed info in this thread: Changing Dynamic URLs to Static URLs [webmasterworld.com]

    Jim

  •