PDA

View Full Version : 301 permanent redirect question



Mr. Pink
09-13-2009, 12:59 PM
Hi all,

I want to put a permanent redirect in my .htaccess file, so that http://mydomain.com/index.php redirects to http://mydomain.com (withotut the index.php ever showing up in the address bar.

I looked all over, and tried everythign, but can't find clear instructions on how to do this.

Can anyone help me out?

thanks...

Chris
09-14-2009, 12:18 PM
RewriteEngine on
RewriteRule ^/index\.php$ http://www.example.com [R=301,L]

That workf or you?

Mr. Pink
09-14-2009, 04:59 PM
Thanks, Chris...

Unfortunately, that didn't work.

I just have to add these 2 lines anywhere into the .htaccess file, right? what's what I did.

Also, I noticed that the permissions for the .htaccess file on my server are 755. Shouldn't they be 644? I tried both, though, but it didn't work.

Chris
09-16-2009, 11:25 AM
Do you know if mod rewrtie is on for your server?

Mr. Pink
09-17-2009, 04:36 PM
I'll find out...


Update: I did find out...

Yes, the hosting company tells me that mod rewrite is on my server.

Does that help, or does it make it more of a mystery?

Mr. Pink
09-24-2009, 07:04 PM
OK, I know that I was the one asking the question, in the first place, so I'll be answering my own question here. This is just for future reference, in case anyone else needs to implement this.

First check if your .htaccess file already has this:

RewriteEngine on

If it doesn't have this line, just add it. Then, after this line add two more lines, so that the whole thing appears as follows:


RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

This worked for me.

Selkirk
09-25-2009, 09:26 AM
I think you can also do this using mod_alias instead of mod_rewrite:



Redirect permanent /index.php http://mydomain.com

Dan Schulz
09-25-2009, 01:33 PM
There's a better way of doing it, one that will work on both Apache 1.x and 2 servers:



RewriteEngine on

# Redirects www. version to non-www version
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.tld [NC]
RewriteRule .? http://yourdomain.tld{REQUEST_URI} [R=301,L]

# Redirects /index.ext to /
RewriteRule ^(.+/)index\.ext?$ http://yourdomain.tld/$1 [R=301,L]


Of course, "yourdomain" "tld" and "ext" are placeholders for your domain name, the top level domain (such as .com or .net) and the file extension (such as .html or .php).

(Credit: David K. Lynn of datakoncepts.com)

Mr. Pink
09-25-2009, 04:51 PM
Hm... That didn't work for me, Dan.

The rewrite for www returns an error message, plus the browser address bar rewrites to this:


http://mydomain.com{request_uri}/

I first thought that there was a typo on this line:


RewriteRule .? http://yourdomain.tld{REQUEST_URI} [R=301,L]

that it should be (URL instead of URI):


RewriteRule .? http://yourdomain.tld{REQUEST_URL} [R=301,L]

but then I still get error and the browser address bar rewrites to


http://mydomain.com{request_url}/



What works for me is this:


RewriteEngine on

# Redirects www. version to non-www version
RewriteCond %{HTTP_HOST} ^www.mydomain\.tld$ [NC]
RewriteRule ^(.*)$ http://mydomain.tld/$1 [L,R=301]

# Redirects /index.ext to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]

I tried this on 2 different servers. Of course, if there was a better way to do it, I would implement it.

Mr. Pink
09-25-2009, 04:54 PM
I think you can also do this using mod_alias instead of mod_rewrite...

Selkirk,

I don't know what using "mod_alias instead of mod_rewrite" means. If it's not too much to ask, can you provide step-by step instructions (for my own future reference)?

Thanks...

Dan Schulz
09-25-2009, 05:00 PM
Are you replacing the placeholders with your actual domain name, top level domain and file extension?

Selkirk
09-25-2009, 06:21 PM
mod_alias is just a different apache module with a simpler syntax. (google will tell all.) Although, using it might have some unintended side effects.

Like http://domain.com/index.php/foo being redirected to http://domain.com/foo.

Mr. Pink
09-26-2009, 03:45 PM
Are you replacing the placeholders with your actual domain name, top level domain and file extension?

Dan,

Yes, I replaced the placeholders, in both cases, with my domain name and the TLD. I double checked that there were no mistakes.

Well, at this point I do have a solution that works. But I was going to implement yours, if it's better. I just thought that perhaps there is a very small typo in the code, in yours, that makes the redirect wrong.

BTW, I want to eliminate the www. and the /index.html (or /index.php) because of duplicate content issues. I recently watched a Google video that was posted on this site, where a Google spokesperson explained that Google sees the following as 6 separate web pages:

http://www.example.com
http://www.example.com/
http://www.example.com/index.html
http://example.com
http://example.com/
http://example.com/index.html

So, Google thinks those are 6 different pages with duplicate content. People linking to our sites are likely to just copy and paste what appears in the browser address bar. So, by implementing these 301 redirects the address bar is forced into always displaying one version of the URL.

Doing this is substantially less work than sending emails to webmasters that had linked in one way, asking them to change these links.