Forum Moderators: phranque
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
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]
Jim
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.
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
@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
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
# 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]
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
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
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
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?
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