homepage Welcome to WebmasterWorld Guest from 54.205.242.179
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess multiple page redirection
Newbie in pain
spiral




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

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]

 

g1smd




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

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...]

spiral




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

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]

g1smd




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

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.

jdMorgan




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

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

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

Jim

spiral




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

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.

jdMorgan




msg:3882827
 12:40 am on Apr 1, 2009 (gmt 0)

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

Thanks,
Jim

spiral




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

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]

spiral




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

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.

Caterham




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

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.

jdMorgan




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

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

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved