Forum Moderators: phranque

Message Too Old, No Replies

Rewriting index.php without rewriting index.php?P=g&Pnr=1

         

glimbeek

11:01 am on Mar 5, 2010 (gmt 0)

10+ Year Member



I'm on a Unix server with Joomla 1.5.x and I use Artio Joomsef to get my SEO URL's.

In Google I can find the following links:
http://www.example.com/index.php
http://www.example.com/index.php?P=g&Pnr=6
http://www.example.com/index.php?P=g&Pnr=9

We don't use index.php?<something> links anymore, so I want to point them to http://www.example.com. Next to that I would like to make sure http://www.example.com/index.php points to www.example.com as well, to prevent the same content on 2 different pages.

In tried the following:
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

Which works.
But then the http://www.example.com/index.php?P=g&Pnr=6 url gets redirected to http://www.example.com/?P=g&Pnr=6

So I could create a redirect just for the "?P=g&Pnr=6" string. But I "can't" do that...

Because of dodgy .htaccess coding in the past the above solution would mean that I would have to rewrite a lot of old RewriteRule's.

So is there a way to rewrite index.php but to also rewrite index.php?P=g&Pnr=6 or index.php?P=ab or /us/index.php?P=n&A=10 ?

**EDIT**

One way to solve the problem is to put the line:
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

All the way at the bottom of the .htaccess file so it rewrites my index.php?P=a rules first. This is seems a bit backward to me...

jdMorgan

10:01 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WebmasterWorld standard non-infinite-redirect-loop index.xyz-to-/ redirect with query string removal:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*index\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.php$ http://www.example.co[b]m/$1? [R[/b]=301,L]

This rule rewrites any request for index.php in any directory to "/" in that same directory, and removes any appended query strings. Note that the trailing "?" on the RewriteRule's substitution URL works as an operator, and is not a literal "?". It will not appear in the redirected-to URL.

Checking THE_REQUEST with the RewriteCond will prevent an infinite redirect/rewrite loop if you ever actually use "index.php" as a filepath on your site again. It guarantees that the following rule will only be executed if it is an HTTP client requesting index.php, and not if index.php is requested as the result of an internal rewrire or DirectoryIndex directive.

Never put external redirects "all the way at the bottom" of your rewriterules unless all other rules are also external redirects. If you do, you will inadvertently 'expose' internally-rewritten filepaths to http clients as URLs.

Two general recommendations:

  • End every rule with an [L] flag, unless you have a good reason not to -- and this is very, very rare.
  • Order your rewriterules with all external redirects first, in order from most-specific pattern and conditions (fewest URLs affected) to least-specific patterns and conditions (several or all URLs affected), followed by all internal rewrites, again in order from most- to least-specific.

    This prevents unexpected rule collisions and operation, and prevents the filepath-exposure problem reviously mentioned.

    Access control rules, if any, should generally precede the redirects: There's no use wasting time or server resources redirecting unwelcome visitors.

    Jim
  • glimbeek

    7:56 am on Mar 8, 2010 (gmt 0)

    10+ Year Member



    jdMorgan,

    Thanks you for the reply. I forgot to mention one thing... which is rather important.

    Some of the old index.php?P=<something> url's actually rewrite to a page, for instance: index.php?P=co rewrites to http://www.example.com/contact/
    or index.php?Us=1 rewrites to http://www.example.com/us/.

    With the above rule everything seems to get rewritten to http://www.example.com/.

    g1smd

    8:07 am on Mar 8, 2010 (gmt 0)

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



    Well, it does that because that's what you asked for.

    You'll need to add a RewriteCond that says to only invoke the RewriteRule if the requested URL does not contain <list>.

    glimbeek

    8:26 am on Mar 8, 2010 (gmt 0)

    10+ Year Member



    Yes I know g1smd and I apologize I should have been more clear about my setup.

    "You'll need to add a RewriteCond that says to only invoke the RewriteRule if the requested URL does not contain <list>."

    The big question is, how do I do that?

    glimbeek

    7:19 am on Mar 11, 2010 (gmt 0)

    10+ Year Member



    Any tips on this jdMorgan or g1smd or anyone for that matter? I'm struggling to figure this one out :/

    glimbeek

    3:21 pm on Mar 12, 2010 (gmt 0)

    10+ Year Member



    The following:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

    Seems to be working. It rewrites index.php and doesn't mess with my other rewrites. Any thoughts about this one?

    g1smd

    12:45 am on Mar 13, 2010 (gmt 0)

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



    That isn't a rewrite. It's a redirect, and it only works for /index.php in the root of the site.

    glimbeek

    7:06 am on Mar 15, 2010 (gmt 0)

    10+ Year Member



    Yeah sorry, a redirect. And that's what it needs to do :) Thanks for the support guys!

    jdMorgan

    2:55 pm on Mar 16, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Your modified code should be fine, as long as you do not use "named anchors" --also called "URL-fragments"-- on your pages. If you do, then you'll need to restore part of the original "trailing sub-pattern" on the RewriteCond:

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*index\.php(#[^?\ ]*)?\ HTTP/
    RewriteRule ^(([^/]*/)*)index\.php$ http://www.example.com/$1? [R=301,L]

    This simply allows the RewriteCond to match (i.e. not fail) if a named anchor is appended to the index.php URL.

    Another solution, if you wish to canonicalize example.com/index.php?P=co to example.com/?P=co, can be accomplished simply by deleting the "?" following the substitution URL in the RewriteRule of the code I posted above, so that the requested query string is not replaced with "blank."

    As always the "correct" code depends on "exactly" what you want to do... :)

    Jim

    glimbeek

    3:02 pm on Mar 16, 2010 (gmt 0)

    10+ Year Member



    Thanks for the reply jdMorgan.

    We refrain from using query strings in any url's or named anchors combined with index.php in the root.

    Cheers for the additional information though!