SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect index.php without parameters

    I would like to ask for help on how to redirect index.php file to homepage if the file doesn't contain any parameters.

    My scenario is:
    http://www.mydomain.com/index.php should redirect (301) to http://www.mydomain.com/
    however
    http://www.mydomain.com/index.php?anyParam=anyValue should not be redirected.

    Thanks in advance.
    Last edited by ScallioXTX; May 31, 2012 at 04:19. Reason: delinkified example URLs

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    IMF,

    You want to remove the DirectoryIndex from the URI? IMHO, that's not a smart thing to do but ... to do so, write some mod_rewrite code to confirm the lack of a query string then redirect index.php to the domain.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I achieved it with this code:
    PHP Code:
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^index\.php?$ http://www.mydomain.com/? [NC,R=301,L] 
    @dklynn
    May I know what you mean by "that's not a smart thing to do"? Does my change have an impact on SEO?

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    IMF,

    Good ... except for the trailing ? in the redirection. What's that supposed to do? Yes, I know that it will remove a query string but you've already tested that a query string doesn't exist! Then there is the No Case flag which is designed to be used on case insensitive variables like {HTTP_HOST}. When you use it with a RewriteRule, you're bound to have problems as it's designed to operate on the {REQUEST_URI} which IS case sensitive.

    "that's not a smart thing to do" referred to

    Quote Originally Posted by IMF
    My scenario is:
    http://www.mydomain.com/index.php should redirect (301) to http://www.mydomain.com/
    'unless a query string exists.'
    First, some hosts configure their systems to show the DirectoryIndex which means that this will loop. Second, you're forcing Apache to look-up the DirectoryIndex in order to select content to respond with. Third, if your goal is to "hide" the DirectoryIndex, a BACK button would likely show it as would links within the website. Why go to all that bother when it's so easy to guess the DirectoryIndex?

    At least you get full marks for not trying to link to http://www.mydomain.com/?anyParam=anyValue!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your time. However, your explanation is beyond to my knowledge since I'm newbie to this and just starting reading about it.

    I'll give the actual structure of my site so that we can fully picture it out.

    I'm using a CMS and it has the following URLs:



    except for the trailing ? in the redirection. What's that supposed to do?
    I just read it somewhere that without it, index.php? will not redirect to main domain so I just test and it does the job so that's why I put it. Is this not necessary at all?

    Then there is the No Case flag which is designed to be used on case insensitive variables like {HTTP_HOST}
    So it's OK to remove the NC part of my code?

    First, some hosts configure their systems to show the DirectoryIndex which means that this will loop. Second, you're forcing Apache to look-up the DirectoryIndex in order to select content to respond with. Third, if your goal is to "hide" the DirectoryIndex, a BACK button would likely show it as would links within the website. Why go to all that bother when it's so easy to guess the DirectoryIndex?
    These are the part I don't really understand at all. Is their a setting in cPanel not to show DirectoryIndex so that it will not loop?


    Right now, I have this full code in .htaccess
    PHP Code:
    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond 
    %{QUERY_STRING} ^$
    RewriteRule ^index\.php?$ http://domain.com/? [NC,R=301,L]

    RewriteCond %{QUERY_STRING} ^page=1$
    RewriteRule ^index\.phphttp://domain.com/? [R=301,L] 
    What are the things I need to remove or append to have a correct code based on my URL structures above?


    I also have a problem with the structure of the category. The first page of the category is correctly done as http: // domain. com/index.php?category=abc. However on page 2 and so on, http://domain.com/index.php?page=2&category=abc, the link to page 1 is structured as http :// domain. com/index.php?&category=abc - there's that extra &. Can you help me what's the code to redirect http :// domain. com/index.php?&category=abc to http: // domain. com/index.php?category=abc (without &)

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    IMFury, there is a much easier way to solve your duplicate content issues.
    http://googlewebmastercentral.blogsp...canonical.html
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    l_c,

    It looks like the canonical code requires that the script be dedicated to the (singular) content.

    IMF,

    Okay, redirect to a single URI is a smart thing to do. Where I have a minor hang-up is stripping the DirectoryIndex off the domain as you do with
    Code:
    RewriteCond %{QUERY_STRING} ^page=1$
    RewriteRule ^index\.php$ http://domain.com/? [R=301,L]
    Yes, the trailing / in the redirection is required to DELETE any pre-existing query string, i.e., page=1, and you specified that correctly. Adding that to index.php does nothing to enable the redirection, only to remove the query string.

    The No Case Flag makes regex case insensitive, i.e., it makes [a-z] have the same effect as [a-zA-Z]. Because Apache requires the exact match (unless you're using mod_speling) to serve a script, it makes no sense to use it in a RewriteRule statement. As I said before, {HTTP_HOST} is not case sensitive so it's designed to work against similar variables which are not case sensitive.

    Hosts can configure their server to show the DirectoryIndex - not many do - so redirecting to remove the DirectoryIndex from the {REQUEST_URI} can cause a loop. Simple statement. IMHO, it's not worthwhile doing this.

    Please use [code]... [/code] wrappers rather than PHP.

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^index\.php?$ http://domain.com/ [R=301,L]
    
    RewriteCond %{QUERY_STRING} ^page=1$
    RewriteRule ^index\.php$ http://domain.com/? [R=301,L]
    Corrected above.

    Ouch! That was a complex explanation to ask to remove a leading & from a query string!

    Code:
    ...
    
    # Remove leading & from query string
    RewriteCond %{QUERY_STRING} ^&(.*)$
    RewriteRule .? %REQUEST_URI}?%1 [R=301,L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    l_c,

    It looks like the canonical code requires that the script be dedicated to the (singular) content.
    There is such a thing called dynamic languages...like php which can change well anything on the page. The canonical url included. Changing it to reflect the current query string is an easy thing to do.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    l_e,

    While this new suggestion/directive could have beneficial effects:

    While the canonical link element has its benefits, Matt Cutts, who is the head of Google's webspam team, has claimed that the search engine prefers the use of 301 redirects. Cutts claims the preference for redirects is because Google's spiders can choose to ignore a canonical link element if they feel it is more beneficial to do so.

    Moreover, all the examples show a single "fishy" page which has been selected, presumably, from a database. That same script can likely pull other items from the database so having the canonical link in the script would not be appropriate for the script (at least when it serves non-fishy content).

    Another link to consider is 3 Reasons to use rel=canonical, and 4 reasons not to. I would lean toward the four reasons not to - that's just my "professionalism" coming to the front.

    Oh, well, just my

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    ...That same script can likely pull other items from the database so having the canonical link in the script would not be appropriate for the script (at least when it serves non-fishy content)....
    Obviously, you change the canonical for the other content...is that so hard to understand? You don't use the "fishy" canonical for "non-fishy" pages you change it to reflect those pages.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    l_e,

    Using a switch statement to cobble-up canonical links for each item in the database seems ridiculous when the other option is to do it right in the first place (as referenced it the four reasons not to use canonical links linked above).

    Thanks for the info, though.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •