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:
[B][noparse]http://www.mydomain.com/index.php[/noparse][/B] should redirect (301) to [B][noparse]http://www.mydomain.com/[/noparse][/B]
however
[B][noparse]http://www.mydomain.com/index.php[/noparse][COLOR=#b22222]?anyParam=anyValue[/COLOR][/B] should not be redirected.

Thanks in advance.

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

I achieved it with this 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?

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

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

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

Options +FollowSymLinks
RewriteEngine On

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

RewriteCond %{QUERY_STRING} ^page=1$
RewriteRule ^index\\.php$ http://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, [B]http://domain.com/index.php?page=2&category=abc[/B], 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 &)

IMFury, there is a much easier way to solve your duplicate content issues.
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

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

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 [noparse]

... 

[/noparse] wrappers rather than PHP.

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!

...

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

Regards,

DK

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.

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 :twocents:

Regards,

DK

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.

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. :tup:

Regards,

DK