Welcome to WebmasterWorld Guest from 54.167.252.62

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess multiple page redirection

Newbie in pain

     
9:36 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



I'm struggling with my .htaccess file to redirect from all pages with index.php in URL to root. Currently have several lines, each doing something and I don't really know anything about .htaccess. so I'm sure what I have is ugly and redundant.

Anyways, what I'm having trouble with is the ability to redirect various pages that have filenames like /index.php?option=com_content&view=article&id=68& ;Itemid=61 to the root of my site. I can't seem to do it. Nothing I've tried worked, so I deleted the attempts.

Here's my .htaccess file so far:

<Files ~ "^.(htaccess¦htpasswd)$">
deny from all
</Files>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)(:80)? [NC]
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
order deny,allow

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

Redirect permanent /cj/consulta/index.php http://example.com
order deny,allow

Redirect permanent /indexbc70.html http://example.com/index70.php?option=com_content&view=ar ticle&id=45&Itemid=54
order deny,allow

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

AddHandler application/x-httpd-php5 .html

Help would be greatly appreciated, as would any pointers to a good tutorial for non-programmers. I've tried a few , but either little of it made sense to me, or it was too basic.

[edited by: jdMorgan at 12:41 am (utc) on April 1, 2009]
[edit reason] example.com [/edit]

9:45 pm on Mar 30, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Don't mix Redirect and RewriteRule within the same file. You'll get strange effects; use RewriteRule for all of the rules.

The Options line needs to go first.

The index redirect needs to go before the non-www redirect.

The very specific page redirects need to go before all of the other redirects.

There's a better way to code the index redirect, and it was posted here only a few hours ago.

[webmasterworld.com...]

11:12 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



Thanks for the reply and suggestions.

I cleaned up the code, but am still having problems. The 1 thing I can't seem to do is redirect a call for www.mysite.com/index.php?option=com_content&view=article&id=58&Itemid=85 into www.mysite.com

What's currently happening is that a call for www.mysite.com/index.php?option=com_content&view=article&id=58&Itemid=85 gets redirected to www.mysite.com/?option=com_content&view=article&id=58&Itemid=85

This is my new .htaccess file:

<Files ~ "^.(htaccess¦htpasswd)$">
deny from all
</Files>
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteRule ^cj/consulta/index.php$ http://www.example.com [R=301,NC]
RewriteRule ^indexbc70.html$ http://example.com/index70.php?option=com_content&view=article&id=45&Itemid=54 [R=301,NC]
RewriteRule ^(.*)index\.php(.*)? http://www.example.com/$1 [R=301,NC]

# Externally redirect requests for index.html in any directory to "/" in that directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

# Externally redirect requests for *all* non-canonical hostnames to canonical hostname, including case errors and appended FQDN indicator and/or port numbers.
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

AddHandler application/x-httpd-php5 .html

Any ideas what I'm doing wrong?

[edited by: jdMorgan at 12:42 am (utc) on April 1, 2009]
[edit reason] example.com [/edit]

11:22 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I'm short on time at present. That code is much better, but there's obviously some issues left still.

One obvious fix is that all redirects need [L] along with the [R=301] flag.

11:24 pm on Mar 31, 2009 (gmt 0)

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



To clear the query string, append a question mark to the substitution URL in the RewriteRule.

RewriteRule ^here$ /ther[b]e?[/b] [flags]

Jim
11:47 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



Thank God for WebmasterWorld, really. Problem finally solved, thanks much for the help jd and g1, it's been oh so frustrating, and I'm feeling a nice sense of relief. Gotta say that you all are quick as well as helpful.
12:40 am on Apr 1, 2009 (gmt 0)

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



Please post your cleaned-up code for the benefit of our members.

Thanks,
Jim

10:27 am on Apr 1, 2009 (gmt 0)

10+ Year Member



OK, so this is what I'm using now, it's doing what I need, but I'm sure it's far from a model ,htaccess file.

<Files ~ "^.(htaccess¦htpasswd)$">
deny from all
</Files>
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteRule ^cj/consulta/index.php$ [mysite.com...] [R=301,NC]
RewriteRule ^indexbc70.html$ [mysite.com...] [R=301,NC]
RewriteRule ^(.*)index\.php(.*)? [mysite.com...] [R=301,L]

# Externally redirect requests for index.html in any directory to "/" in that directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ [mysite.com...] [R=301,L]

# Externally redirect requests for *all* non-canonical hostnames to canonical hostname, including case errors and appended FQDN indicator and/or port numbers.
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]

AddHandler application/x-httpd-php5 .html

------------------------------------
Now let's say I wanted the site to redirect from www to non-wwww pages, is this the way?

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]

10:40 am on Apr 1, 2009 (gmt 0)

10+ Year Member



Something else I'm not clear about - on this line:
^cj/consulta/index.php$ [mysite.com...] [R=301,NC]

I did not escape the "." in [index.php], but it still seems to work. So I don't think I understand when escapes are needed. I though that in a string between ^ and $ any special character needed escaping.

5:43 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



but it still seems to work

Of course since the period is "any character", too. If you escape it, it won't match any character but a literal period.

So I don't think I understand when escapes are needed.

Any time when something which has a special meaning in a specific environment shouldn't have its special meaning.

6:08 pm on Apr 1, 2009 (gmt 0)

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



You can improve the performance of your rules by adding the [L] flag to each of them -- as in "[NC,R=301,L]"

Jim

 

Featured Threads

Hot Threads This Week

Hot Threads This Month