Forum Moderators: phranque

Message Too Old, No Replies

Redirect index.html and remove file extensions not working

         

zee007

2:07 pm on Mar 11, 2014 (gmt 0)

10+ Year Member



hi,

I've been banging my head against the wall trying to figure this one out. I would like to do the following:

1. Direct www.homepage.com/index.php to redirect to www.homepage.com ONLY

2. Remove any .php or any type of extensions from the URLS and have them redirect to the same page.

Can someone help me out here?

This is what I'm currently using:

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]


This correctly re-directs the www.homepage.com/index.php to www.homepage.com. But what it also does is redirect any other page without the .php extension to the homepage also. All other pages without the .php extension should redirect back to the original page and not the homepage by default.

Any suggestions?

g1smd

8:31 pm on Mar 11, 2014 (gmt 0)

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



The rule above could do with some tidying of syntax but is not responsible for anything other than redirecting index requests.

Are you sure there are no other rules?

I assume there is a
RewriteEngine On
statement somewhere too?

lucy24

9:43 pm on Mar 11, 2014 (gmt 0)

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



RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

This correctly re-directs the www.homepage.com/index.php to www.homepage.com. But what it also does is redirect any other page without the .php extension to the homepage also.

No, it doesn't. Honest. Some other code elsewhere may be redirecting requests to the homepage, but the quoted RewriteRule does not and cannot do it. The quoted rule only applies to requests containing the explicit text "index.php". (Or indexwphp or index1php or...)

btw, you never need the locution
^.*
Omit both the anchor and the .* when you're not capturing.

It's possible that you've misunderstood what ^ means. I was recently rereading an older thread where this turned out to be part of the issue.

zee007

10:10 pm on Mar 11, 2014 (gmt 0)

10+ Year Member



Thanks for helping out guys. I'm not that well versed with this, so I've posted everything from my HTACCESS below. Basically, my goal is to (a) re-direct www.example.com/index.php to www.example.com and to (b) re-direct all other pages that end with .php to their respective pages such as www.example.com/about.php to www.example.com/about

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit POST PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName www.example.com

ErrorDocument 500 http://www.example.com

ErrorDocument 404 http://www.example.com

ErrorDocument 403 http://www.example.com

ErrorDocument 401 http://www.example.com

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

lucy24

11:42 pm on Mar 11, 2014 (gmt 0)

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



ErrorDocument 500 http://www.example.com
ErrorDocument 404 http://www.example.com
ErrorDocument 403 http://www.example.com
ErrorDocument 401 http://www.example.com


Noooo! This is wrong in two separate and unrelated ways.
#1 an ErrorDocument directive must never contain an explicit domain name, because that converts the response into a 302 redirect. (This is actually true of any Apache directive that involves a "target".) Instead it should start with a single / representing the root.
#2 you seem to be redirecting everyone to the front page. Search engines and users both hate this, independent of whether it's configured as a redirect or you're using / as your error document.

Besides, why on earth would you send 403s to the front page? If nothing else, it leads to an infinite loop.

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The first rule-- the "index.php" redirect-- will not be executed, because it comes before the "RewriteEngine On" directive. The second rule is a domain-name-canonicalization redirect. The Condition should be expressed as
!^(www\.example\.com)?$
meaning "anything other than my desired form".

You must have another htaccess file somewhere, since there's absolutely nothing in the quoted part that does
#1 what you say you want (redirect .php requests to extensionless)
#2 what you don't want but is happening anyway (redirect extensionless requests to homepage).

Are you positive there isn't a CMS-boilerplate htaccess in some subdirectory? "Redirect all extensionless URLs to the root" is exactly what you'd expect in a built-in CMS htaccess file-- the kind that comes with WordPress, Joomla, Drupal and assorted others I can't think of.

zee007

12:19 am on Mar 12, 2014 (gmt 0)

10+ Year Member



Hi,

Nope, this site is not a CMS based one. The .php and the URL canonical directives were put there by myself (obviously not correctly). The ErrorDocument stuff was there from before I began work on the website.

Any chance you can provide me the right code I can put in the HTACCESS file to do what I had outlined earlier?

Cheers!

lucy24

3:01 am on Mar 12, 2014 (gmt 0)

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



Well, not cut and paste. Wrong forum. You could try over on That Other Forum, which is more oriented towards questions-and-answers. They do get a bit snarky if you ask a question that has already been answered, but that's what search engines are for.

The .php and the URL canonical directives were put there by myself (obviously not correctly).

It isn't so much "not correctly" as not at all. There simply is nothing in the pasted-in code that says anything about extensionless URLs, one way or the other.

zee007

1:45 pm on Mar 12, 2014 (gmt 0)

10+ Year Member



What "other forum"? And I have seen many posts on here that have provided cut and paste solutions.

zee007

1:51 pm on Mar 12, 2014 (gmt 0)

10+ Year Member



Also, I've come across an issue on another website.

I'm finding this version of the homepage:

www.example.com/home.php?

How do I redirect this back to www.example.com. Can I just do a simple 301 re-direct?

Cheers

g1smd

7:27 pm on Mar 12, 2014 (gmt 0)

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



You can redirect but you will need a RewriteRule with a preceding RewriteCond looking at the THE_REQUEST to prevent an infinite loop.

lucy24

10:10 pm on Mar 12, 2014 (gmt 0)

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



Is "home.php" the name of your directory index file? If so, you need either a RewriteCond or an [NS] flag. If the real index page is called something else, you don't need the condition.