Forum Moderators: phranque

Message Too Old, No Replies

Problems with mod Rewrite in .htaccess

         

pftodd

4:48 pm on May 12, 2010 (gmt 0)

10+ Year Member



Hello

I want to rewrite my urls such that ...
  • /stats goes to /statistics.php
  • /about goes to /about.php
  • / goes to index.php
  • however, also that a given variable, e.g. '/A6gT5s' goes to /url.php?pft=$1

    My code is ...

    RewriteEngine On
    RewriteRule ^$ index.php [L]
    RewriteRule ^/$ index.php [L]
    RewriteRule ^stats$ statistics.php [L]
    RewriteRule ^about$ about.php [L]
    RewriteRule ^([^/]+)$ url.php?pft=$1 [L]


    However, these cannot seem to be used in conjunction with each other. I notice websites like bit.ly actively do this, so what would they have in their .htaccess?

    Thanks very much,
    Patrick
  • g1smd

    6:36 pm on May 12, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Change
    ^([^/]+)$
    to
    ^([^/.]+)$
    and it will no longer match url.php, and stop your infinite loop.

    pftodd

    7:28 pm on May 12, 2010 (gmt 0)

    10+ Year Member



    Thanks that seems to have worked! What do you mean by 'stop your infinite loop'?

    Thanks.

    g1smd

    7:41 pm on May 12, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    The rewrite target matched the pattern and would rewrite again, and match again and rewrite again, and again and again.

    jdMorgan

    12:32 am on May 13, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Note also that

    RewriteRule ^$ index.php [L]
    RewriteRule ^/$ index.php [L]

    could be compressed to

    RewriteRule ^/?$ index.php [L]

    Although technically your original second rule will only match requests for example.com// (two slashes), and really, you should redirect that to remove the second slash instead of accepting it and rewriting it to index.php.

    Jim