Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

removing index.php from 301 redirect

     
7:47 pm on Aug 11, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2003
posts:160
votes: 0


I am using .htaccess to create a 301 redirect that directs http://www.example.com to mysite.com/index.php. I need to remove the index.php from the end of the url and cannot figure out how to do so.

This is what I currently have written:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^([^/]+)/$ http://example.com/$1 [L,R=301]

If anyone could help and let me know what changes I need to make that would be great.

[edited by: jdMorgan at 8:05 pm (utc) on Aug. 11, 2008]
[edit reason] example.com [/edit]

8:02 pm on Aug 11, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Do you have another rule somewhere that is rewriting or redirecting "example.com/" to "example.com/index.php" ?

This function is usually done using the DirectoryIndex directive, but if you've done it with a rewrite or a redirect, then that directive and your rule are likely interacting. Otherwise, there is no reason for /index.php to appear in the URL.

Jim

8:49 pm on Aug 11, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2003
posts:160
votes: 0


I am not so familiar with .htaccess, but this is what my entire file looks like.

#DirectoryIndex index.php index.html

#Options +FollowSymLinks
#Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /relative/web/path/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]

RewriteRule ^(.*) index.php

RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>

#php_value register_globals 0

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L]

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^([^/]+)/$ http://example.com/$1 [L,R=301]

9:28 pm on Aug 11, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


OK, that could use some improvement:

DirectoryIndex index.php
#
Options +FollowSymLinks
RewriteEngine on
#
# Redirect to remove "<any_directory>/index.php" from search results
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php
RewriteRule ^(([^/]+/)*)index\.php$ http://example.com/$1 [R=301,L]
#
# Redirect to force canonical non-www domain name
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
# If Authorization is not blank, copy it to Remote_User
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
#
# If requested URL does not resolve to existing file or directory, rewrite it to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I removed one rule because it wasn't clear what it was for, and it seemed to create an 'infinite' redirection loop.

The HTTP_Authorization rule is not likely to work, since mod_rewrite usually cannot be used to modify server variables -- I don't think you can change "Remote_User".

I removed the <IfModule> container, since the code would fail silently if mod_rewrite wasn't installed and that container was in place.

I re-ordered your rules so that the most-specific redirect rule is first, followed by the less-specific redirect, followed by the internal rewrite. This order prevents stacked or chained redirects, and prevents the redirects from "exposing" your index.php file.

Jim

[edited by: jdMorgan at 9:30 pm (utc) on Aug. 11, 2008]

9:54 pm on Aug 11, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2003
posts:160
votes: 0


Wow, I was not expecting this level of help, thank-you so much!

Unfortunately when I modified my .htaccess file my non www domain, and all my internal pages gave me an error that said:

"The page isn't redirecting properly"

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing to accept cookies.

I think my CMS may be the reason this is not working.

Thanks again,
Jeremy

10:02 pm on Aug 11, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Make sure your CMS is not generating a 30x redirect to the www.example.com domain. If it is, you'll get a loop.

Since you are running FireFox, it would be a simple matter to install the "Live HTTP Headers" add-on and see if that is what is going on.

Jim

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members