Forum Moderators: phranque

Message Too Old, No Replies

several [R=301,L]

redirects

         

akyprian

6:11 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



This is my first post here, so hi everybody.

I 've only dealt with redirects lately and I 've managed to make some work:

#http://example.net/ is riderected to http://www.example.net/
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(net) [NC]
RewriteRule (.*) [%1.%2...] [r=301,L]

#http://www.example.net/index.php is redirected to www.example.net/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.net/ [R=301,L]

Please help me improve them, if you think so.

In addition, I need to do some extra redirections, but failing miserably!

1. http://www.example.net/index.php? should be redirected to http://www.example.net/

2.I prefer http://www.example.net/forum/index.php as my forum root.

There are 2 cases I want to redirect [301] to the above url. The two urls that must be redirected are:

a. http://www.example.net/forum/index.php?act=idx

b. http://www.example.net/forum/index.php?

Can you provide some help, please?

Best Regards,

Antonis Kyprianou

jdMorgan

6:49 pm on Mar 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Antonis,

Welcome to WebmasterWorld!

Add a RewriteCond to detect "/forum/index.php?" and /forum/index.php?act=idx" cases.

Now reverse the rules to avoid 'stacked' redirects if "example.net/index.php", "example.net/forums/index.php?" or "example.net/forums/index.php?act=idx" is requested. (If one of those URLs was requested with the rules in their original order, you would get two 301 redirects: one to fix the non-www domain, then another one to remove "index.php". This could easily confuse search engine spiders.)

Finally, simplify some of the notation:


# redirect /index.php,
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ [OR]
# /forum/index.php?, or /forum/index.php?act=idx to www.example.net/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?(act=idx)?\ HTTP/
# if URL-path ends with "index.php" (URL-path in RewriteRule never includes the query string)
RewriteRule index\.php$ http://www.example.net/? [R=301,L]
#
# redirect <anydomain>.net<any_path> to www.<anydomain>.net<any_path>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.net [NC]
RewriteRule (.*) http://www.%1.net/$1 [R=301,L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

youfoundjake

6:51 pm on Mar 26, 2007 (gmt 0)

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



1. http://www.example.net/index.php? should be redirected to http://www.example.net/

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

and for

2.I prefer http://www.example.net/forum/index.php as my forum root.

Try putting a .htaccess in your forum folder that also does the same thing...

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

Welcome to WebmasterWorld, and jim will surely correct me where im wrong, and school all of us.

*edit* dang jim's smart. saw his post after mine

akyprian

9:43 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



Thanks for the quick reply and the welcoming!

@jdMorgan:
I am afraid, there is a misunderstanding:

I don't want:
# /forum/index.php?, or /forum/index.php?act=idx to www.example.net/

but
# /forum/index.php?, or /forum/index.php?act=idx to www.example.net/forum/index.php

Can you please help a bit more?

@youfoundjake:
I 'll try your solution in a while and let you know, thanks a lot.

Antonis

akyprian

10:00 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



@youfoundjake:

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

causes:
www.example.net/index.php? goes to www.example.net/? which is wrong. Also, it produces wrong redirections to my /forum pages

Can I avoid the extra .htaccess in the forum folder?

Thank you for your valuable assistance

Antonis

jdMorgan

10:38 pm on Mar 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you must be very specific with mod_rewrite. I encourage you to read the documents I cited above and experiment with this code so that you understand it and can customize it yourself.

# redirect "/index.php" to "www.example.net/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.net/? [R=301,L]
#
# redirect "/forum/index.php?" or "/forum/index.php?act=idx" to "www.example.net/index.php"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?(act=idx)?\ HTTP/
RewriteRule ^forum/index\.php$ http://www.example.net/index.php? [R=301,L]
#
# redirect <anydomain>.net<any_path> to www.<anydomain>.net<any_path>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.net [NC]
RewriteRule (.*) http://www.%1.net/$1 [R=301,L]

Jim

akyprian

11:48 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



That was great! I think I got it. All seems to be working as expected.

just to verify, is the following "optimal"? (Iam concerned if the two consequtive "?_?" are ok :

# redirect "/index.php" or "/index.php?" to "www.example.net/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?_?\ HTTP/
RewriteRule ^index\.php$ http://www.example.net/? [R=301,L]

Thanks pointing me to the right direction and the links.

Cheers,

Antonis

jdMorgan

12:14 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I am concerned if the two consequtive "?_?" are ok:

I don't know, because you did not describe this in your original posts, so I do not know what you are trying to do.

"php\?_?\ HTTP/" means "php" followed by a literal question mark, followed by an optional underscore, a literal space, and "HTTP/" followed by anything (because the pattern is not end-anchored).

So that would match "php?_ HTTP/1.0" or "php? HTTP/1.1" for example.

If that is what you wanted, then the pattern is correct.

Jim

akyprian

12:21 am on Mar 27, 2007 (gmt 0)

10+ Year Member



?_? means one? and then another? but the forum does not allow me to post two? sequentially!

got it?

jdMorgan

12:26 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"php\??" would mean "php followed by one or zero literal question marks" and is valid.

Note that I used the BBcode [webmasterworld.com] equivalent of "?<b></b>?" to override the forum restrictions on sequential questions marks. This also works to force the forum to retain spaces preceding the "!" character.

Jim

akyprian

12:28 am on Mar 27, 2007 (gmt 0)

10+ Year Member



Jim,

You are a great help, thank you very much indeed.

Antonis

akyprian

6:15 am on Mar 27, 2007 (gmt 0)

10+ Year Member



Hi again,

One big problem I am facing with Jim's code:

# redirect "/forum/index.php?" or "/forum/index.php?act=idx" to "www.example.net/forum/index.php"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /forum/index\.php\?(act=idx)?\ HTTP/
RewriteRule ^forum/index\.php$ http://www.example.net/forum/index.php? [R=301,L]

Everything works as expected but there is also an unexpected behaviour:

The http://www.example.net/forum/index.php? url is sometimes used by the forum as an intermediate step before going to another url. For example, trying to to post (with an attachment-zip) in the forum will firstly lead to http://www.example.net/forum/index.php? and after 1-2 seconds the user is redirected (by the forum) to the url of the newly created post. Due to the mod_rewrite above, the post fails to upload. Is there any workaround for this or I have to live with http://www.example.net/forum/index.php?

jdMorgan

12:34 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because HTTP is a stateless protocol, the server has no "memory" of past HTTP requests, and these cannot affect current or future requests unless you use a script to record information about the request history for later use.

I'd suggest modifying the script to eliminate this "intermediate step," since it sounds like a major inefficiency in the design, slows down the user experience, and leads to the unnecessary extra step (intermediate URL) that is causing problems.

Jim

akyprian

8:45 am on Mar 28, 2007 (gmt 0)

10+ Year Member



Thank once again, Jim

Antonis