301 Redirect index.php to domain (root)

Been trying to get a certain domain to redirect from http://www.example.com/index.php to http://www.example.com/ so that it doesn’t show up as a duplicate page within the search engines.

I have tried a ton of different examples with the current markup I have in my .htaccess file. I have listed my current code below:

RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

# mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

I have added this to try the redirect and it does nothing:

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

Any ideas?

Thanks!

MJ,

First, why would you care to redirect FROM the script you WANT to serve and make Apache go determine the DirectoryIndex before serving the same file?

Okay, now that’s out of my system … your problem may be with your host’s server configuration (in other words, it may be programmed to display the DirectoryIndex for you). If not …

RewriteEngine On
RewriteBase /blog/
[indent]The RewriteBase directive sets the base URL-path (not the physical directory path) for per-directory (.htaccess) rewrites. This directive is required for per-directory (.htaccess) rewrites whose context is a directory made available via the Alias directive.

If your URL path does not exist on the filesystem or is not contained in your DocumentRoot, you must use RewriteBase in every .htaccess file where you want to use RewriteRule directives. Because webmasters are not likely to be using a mod_alias redirection and mod_rewrite from an .htaccess file, RewriteBase is not necessary.

In other words, that's a bad thing to use since you aren't redirecting with mod_alias, etc.[/indent]
RewriteRule ^index\\.php$ - [L]
[indent]If the {EWQUEST_URI} is index.php, pass it through - otherwise ...[/indent]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
[indent]If the request is not a file and not a directory, redirect everything but an empty {REQUEST_URI} string to blog/index.php - in other words, you missed the intent of your redirection! Make that single character optional.[/indent]
# mod_rewrite in use
Options +FollowSymlinks
[indent]HOW is the above going to work without this - which is probably already set in the server config file? DELETE.[/indent]
RewriteEngine On
[indent]Ditto - it's already on (OUT of comment mode) or the above code wouldn't work.  Either move this before your mod_rewrite code or DELETE it.[/indent]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
[indent]That just enforces www in the domain name - the hard way.  Use RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L] instead.[/indent]

I have added this to try the redirect and it does nothing:

Options +FollowSymLinks
[indent]Ditto the above - WHY TWICE?[/indent]
RewriteEngine on
[indent]Ditto the above - WHY TWICE?[/indent]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /index\\.php\\ HTTP/
[indent]It seems that the people who try to use {THE_REQUEST} don't know how to do it - or even what they are attempting to match. {THE_REQUEST} is formatted as "GET /index.html HTTP/1.1" so you've CORRECTLY addressed the space issues in the string (with backslashes) but to what advantage? If all you care about is the index.php, eliminate the GET/POST, HTTP/ and the spaces and merely match /index\\.php.[/indent]
RewriteRule ^index\\.php$ http://www.example.com/ [R=301,L]
[indent]Because of the pass through's Last flag at the beginning of the mod_rewrite code, this will never be matched.  THAT is your problem.

Any ideas?

Correct ordering of your mod_rewrite directives. I start by stating what I want my mod_rewrite code to do then code that. Please reply with a “specification” of what you are intending for your mod_rewrite code to do for you.

Thanks!

Regards,

DK

Sorry about the belated response to this post… I was in the hospital at the time awaiting my son to be born! So, I do appreciate your patience if you can get still respond to me. :slight_smile:

I know nothing when it comes to mod_rewrite and exactly what I can do with an .htaccess file… Here is what I am wanting to do:

MJ,

CONGRATULATIONS! I hope you can find a “football” team in St Louie to teach him a bit of … er, soccer!

Force www is in my signature’s tutorial - simple as can be!

Eliminate the DirectoryIndex can be a more difficult problem as Apache is supposed to serve that file so removing it from the location box can be problematic (and far more trouble than it’s worth).

Ditto the DirectoryIndex in a subdirectory.

If you REALLY want to pursue the second and third anyway, please let me know. It might also help to understand WHY you want to do that, too.

Regards,

DK

David,

Thank you, thank you! We do have quite a few premiere youth programs here in St. Louis along with a minor league soccer club… Hoping for enough interest to turn it into an MLS club someday!

The reason I want #2 and #3 is to negate the pages as showing up as duplicate content. I know it is a pretty highly contested subject, but for SEO purposes I would rather have those redirects available. I guess I could use a canonical link though… Hmmmm… Just thought of that.

MJ,

While you probably don’t like “indoor soccer,” I’d played in an over 30’s league a few years ago in San Diego (along with the outdoor variety). I have to say that the sheer speed of the indoor game made it far more exciting than outdoor although I had to give-up both after dislocating my finger (not a good thing for a keeper to do) - repeatedly.

Okay, back to your “remove the file from the location box” problem:

  1. Make sure your DirectoryIndex has index.php in its list (preferably first).

  2. Check {THE_REQUEST} (normally “GET /index.php HTTP/1.1”) the index.php file(s) and only redirect if NOT matched.

Regards,

DK

David,

Actually, I do play in an over-30 league here with my brother and some of our college friends. I miss outdoor, but far too out of shape to keep playing right now.

I’m still confused on the mod_rewrite stuff. Don’t really understand exactly how the code is supposed to work…

Thanks for all of the help so far.

MJ,

Actually, I was about 40 when I last played - I won’t even suggest how long ago that was.

As for how to redirect to eliminate the index.php from the location box:

RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"
RewriteRule index\\.php http://your.domain.com/ [R=301,L]

Merely insert the other subdirectory before index.php in the RewriteCond statement AND in the RewriteRule, i.e., duplicate both for the subdirectory condition).

Regards,

DK

David,

I haven’t quite hit 40 yet, but I couldn’t imagine still trying to play competitively at that age still unless I get my tail in shape again. :slight_smile:

Thanks for the help again! Now that makes more sense to me. With mod_rewrite I need it written for me to see and understand it.

So to summarize:


RewriteEngine On
RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"
RewriteRule index\\.php http://your.domain.com/ [R=301,L]

RewriteCond %{THE_REQUEST} "GET /blog/index.php HTTP/1.1"
RewriteRule blog/index\\.php http://your.domain.com/blog/ [R=301,L]

RewriteCond %{http_host} ^example.com [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

This should redirect index.php to the main directory or sub-directory and redirect domain.com to www.domain.com. Correct?

MJ,

As a keeper, I didn’t have to run all over the field - just move out of the box 'cause my backs were more interested in offside calls AT MIDFIELD than defending (or so it seemed) so I had to play “outfield” and be able to fall back to play keeper when necessary.

Now, back to coding. I may confuse you just a bit but, because we DON’T need all the garbage in {THE_REQUEST}, I’ll combine the first two RewriteRules into one; the third will only have to act on anything not getting redirected by the first:

RewriteEngine on
RewriteCond %{THE_REQUEST} /index.php
RewriteRule ^(blog/)?index\\.php$ http://www.your.domain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^your.domain.com [NC]
RewriteRule .? http://www.your.domain.com%{REQUEST_URI} [R=301,L]

YOU must replace “your.domain.com” with your own domain’s name!

The only “trick” in the first RewriteRule is that the RewriteCond is ONLY checking for /index.php (so it will match index.php in EVERY directory on the site so I’ve assumed only the blog directory and {DocumentRoot}). The RewriteRule will then capture the (OPTIONAL) “blog/” and use it in the redirection. If you want to do this for ALL your directories, use regex like “([a-z]+/)” to replace “(blog/)”.

Regex is a very powerful tool so just a little time spent understanding it will pay great benefits.

Regards,

DK

David,

I can understand your pain! I played sweeper and as a central defending midfielder for my high school and college days. I was always concerned with putting the least amount of pressure on my keeper, so I did my job and did it well. Ha ha! I protected my keeper at all costs and at all times.

Your help has been a blessing in disguise!!! I’m starting to understand this now. Thank you again and again. I guess when I order my reading materials for the upcoming year I need to invest in some Apache material that will teach me mod_rewrite. :slight_smile:

Hopefully I will post the changes next week when I have a chance.

MJ,

Pain? I got my eye socket “caught” by a header at the top of the box - I hadn’t learned to throw elbows when under attack and learned the hard way. I didn’t want to use dirty gloves to wipe away the blood but had a good chuckle when I overheard one of the other team saying that I was trying to intimidate them by leaving the blood trickling down my face. Of course, the “shiner” lasted for about a week, too, but that was a “badge of honor.”

Regex? Did you READ the tutorial article linked in my signature? It’ll give you all you need to know (especially with the many coding examples actually solving common problems) about developing good regex. Don’t miss the couple of links at the bottom of that VERY LONG page, too, as the “cheat sheet” and Regex Coach are both beneficial. Better yet, download the PDF version and review it whenever you need to (and check my signature here for updates).

As only a subset of regex is used by mod_rewrite, it would be GROSS OVERKILL for me to recommend the regex bible: “Mastering Regular Expressions” by Friedl. However, there is no expert like Friedl if you’re serious about learning all there is to know about regular expressions.

Regards,

DK

David,

Once again thank you!

Enjoyed the story too! Wish I had the time and I would share a few, but this also wouldn’t be the place. :slight_smile:

Thanks again,
Michael

Hey David,

I just tried using that mod_rewrite code for a different website with the same structure and when I use it, it absolutely ruins access to the blog. The www redirect and index.php redirect work perfect on the main domain, but once you click on the link to domain.com/blog/ it gives an error regarding the redirect.

It has to be some setting within the way the site was installed. Not sure if you can put a finger on it.

Thanks again,
Michael

EDIT: For above post…

I found out the problem. It was a setting in Wordpress that was causing the redirect errors.

mj,

Aw, you’ve GOT to show me the entire mod_rewrite for me to assess what’s going on - and a test URI would help!

Oh, yeah, kill the index.php request? ARGH! I definitely have to see what you’re trying to do!

Regards,

DK

mj,

You beat me to a response. Care to share so others won’t fall into the same trap?

Regards,

DK

Wordpress has a setting to change the URI of the blog. So some people leave it as domain.com while others use the www.domain.com. Wordpress actually manages its own redirects. The only mod_rewrite that is needed is if you decide to use permalinks and the code is then generated for you to add if Wordpress cannot access your .htaccess file.

I simply used the final code you gave me and inserted the domain name applicable. It works just fine!

Thanks!

Whew!

Regards,

DK