Forum Moderators: phranque

Message Too Old, No Replies

301 Redirection in .htaccess

         

Diablotik

10:14 am on May 15, 2009 (gmt 0)

10+ Year Member



I need to have redirected everything what is after "/".
Address is http://example.com and I want to have redirected everything to [forum.example.com...] but http://example.com must be untouched as this is my main page.
So for example I need to have this link redirected:
http://example.com/topics33/nowy-system-pochwal-vt165.htm
and I need it to be
[forum.example.com...]

Is anyone able to help please?

[edited by: jdMorgan at 3:29 pm (utc) on May 15, 2009]
[edit reason] Please use example.com only. See TOS. [/edit]

Nick_oTeane

10:24 am on May 15, 2009 (gmt 0)

10+ Year Member



RedirectMatch (.*)\.(htm¦html)$ [forum.example.com...]

Somehow this forum messes up the delimiter between htm+html. Got to be a l of course ! ;)

[edited by: Nick_oTeane at 10:41 am (utc) on May 15, 2009]

[edited by: jdMorgan at 3:30 pm (utc) on May 15, 2009]
[edit reason] example.com [/edit]

Diablotik

10:28 am on May 15, 2009 (gmt 0)

10+ Year Member



I will check it as soon as I get home but also how can I make it 301 redirection?

RedirectMatch (.*)\.(htm¦html)$ [forum.angliki.info...] [R=301,L]

Is this gonna work?

Nick_oTeane

10:53 am on May 15, 2009 (gmt 0)

10+ Year Member



you could try...

RedirectMatch 301 (.*)\.(htm¦html)$ [forum.example.com...]

[edited by: jdMorgan at 3:30 pm (utc) on May 15, 2009]
[edit reason] example.com [/edit]

Diablotik

3:21 pm on May 15, 2009 (gmt 0)

10+ Year Member



OK, firstly tried
RedirectMatch (.*)\.(htm¦html)$ [forum.example.com...] but it did not work at all. So I removed html from the code so it looked like that:
RedirectMatch (.*)\.(htm)$ [forum.example.com...]
and then it was redirecting me to [forum.example.com...]
as there was probably $1 missing at the end so I added /$1 so it looked like that:
RedirectMatch 301 (.*)\.(htm)$ [forum.example.com...]
but then it was creating urls with double "//" so I removed "/" from the code so it looked like that:
RedirectMatch 301 (.*)\.(htm)$ [forum.example.com$1...]
and this is my final piece of code but it does not adding ".htm" at the end of the url.

Any ideas?

[edited by: jdMorgan at 3:31 pm (utc) on May 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:38 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The variables $1 through $9 refer to the part of the requested URL-path matching the first through ninth parenthesized sub-expressions in the pattern, respectively. So you included $1 in the new URL, but you did not include $2, and so the "htm" got dropped.

You don't need two sets of parentheses anyway. Take advantage of the pwoer of regular expressions, and use


RedirectMatch 301 ^/(.+\.html?)$ http://forum.example.com/$1

instead. This will match either "htm" or ".html" at the end of the requested URL.

If "forum.example.com" is *not* hosted in a separate filespace on this server, then this directive will cause a redirection loop, and you will have to use mod_rewrite instead of mod_alias to fix that problem.

Jim

Diablotik

3:52 pm on May 15, 2009 (gmt 0)

10+ Year Member



As you have mentioned this line is causing redirection loop. I do not understand much about this code but my redirected link looks like that now:

[forum.example.com...]

The link it was redirected from is http://example.com/247.htm

Can you help?

[edited by: jdMorgan at 8:00 pm (utc) on May 15, 2009]
[edit reason] Use example.com only. [/edit]

jdMorgan

7:59 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mod_rewrite in example.com/.htaccess :

Options +FollowSymLinks -MultiViews
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(.+\.html?)$ http://forum.example.com/$1 [R=301,L]

Jim

[edited by: jdMorgan at 8:00 pm (utc) on May 15, 2009]

Diablotik

8:11 pm on May 15, 2009 (gmt 0)

10+ Year Member



Thanks jdMorgan. It works like a charm now.
I have very last question. What does this line mean:
Options +FollowSymLinks -MultiViews
?
I have some other piece of coding there and I would not like to mess it up. Thanks

jdMorgan

8:35 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Enabling FollowSymLinks is required to use mod_rewrite.
Disabling MultiViews prevents mod_negotiation from interfering with mod_rewrite.

If you cannot figure out how to "merge" the two Options lines, then post your original Options lines here.

Jim

Diablotik

10:42 am on May 16, 2009 (gmt 0)

10+ Year Member



It is working OK now but I have another question - that one should be fairly easy.
How can I redirect specific url to another?
I would like to redirect [forum.example.com...] to http://example.com

jdMorgan

9:45 pm on May 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the code above as a start, what can you come up with? We're happy to help you here, but we can't write the code for you -- too many askers and not enough contributors for that.

Jim

Diablotik

11:49 pm on May 16, 2009 (gmt 0)

10+ Year Member



Well I gave it a try and made that line up:
Redirect 301 /portal.htm [forum.example.com...]
and this is quite good but I would like that if there is more text after /portal.htm it would redirect with that line.
I have difficulties what to put after portal.htm

Redirect 301 /portal.htm*here?* [forum.example.com...]

[edited by: Diablotik at 12:37 am (utc) on May 17, 2009]

Diablotik

12:55 am on May 17, 2009 (gmt 0)

10+ Year Member



Sorry I think I completly messed that one up. It is quite late now and I still cannot figure it out :(

What I read on this forum I think I should use RewriteRule instead of Redirect if this is a dynamic page.
So I think it should be similar to

RewriteRule ^/portal.htm$ http://www.example.com/$1 [L,NC]

but it does not work :(
I cannot find out how to rewrite the rest of the url which is after portal.htm to new url as this is probably the reason why this does not work.

[edited by: Diablotik at 1:02 am (utc) on May 17, 2009]

jdMorgan

1:54 am on May 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We need to know what "more text after /portal.htm" looks like...

If it is a query string, then you'll have to use a RewriteCond if you want to change that query string. If you want to pass the query string through, then no additional code is needed, as this is the default behaviour.

However, note that the pattern is different between Redirect and RewriteRule in .htaccess :


RewriteRule [b]^po[/b]rtal\.htm$ http://www.example.com/ [NC,R=301,L]

Jim

[edited by: jdMorgan at 1:55 am (utc) on May 17, 2009]

Diablotik

9:27 am on May 17, 2009 (gmt 0)

10+ Year Member



Thanks for your reply once again. After /portal.htm is query string which looks for example like that:
http://example.com/portal.htm?strona=2
and I would like to pass the query string through but it does not work with the code provided above.
Maybe because on the beggining of the .htaccess file there is a rule that is redirecting my original portal.php file to portal.htm?

jdMorgan

3:21 pm on May 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We need to see your code if you want to ask questions about it.

If you write a rule like

RewriteRule ^abc$ /def [L]

then any query string appended to /abc in the original request will be passed to /def with no change. This is the default behaviour.

If you write a rule like

RewriteRule ^abc$ /de[b]f?[/b] [L]

then any query string appended to /abc in the original request will be removed (replaced with blank). It will not be passed to /def.

If you write a rule like

RewriteRule ^abc$ /def[b]?new-query=value[/b] [L]

then any query string appended to /abc in the original request will be replaced with "new-query=value", and the result will be passed to /def.

If you write a rule like

RewriteRule ^abc$ /def?additional-query-parameter=value [[b]QSA,[/b]L]

then "additional-query-parameter=value" will be appended to any query string already attached to /abc in the original request, and the result will be passed to /def.

If you want to test a query string, then you must use a RewriteCond:

 # Replace old query, keep same "page" URL
RewriteCond %{QUERY_STRING} ^old-query=old-value$
RewriteRule ^abc$ /abc?new-query=new-value [L]

Jim

g1smd

11:16 am on May 21, 2009 (gmt 0)

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



How are you getting on with this?

Diablotik

10:55 am on May 22, 2009 (gmt 0)

10+ Year Member



To complicated for me so I passed on this and blocked portal.php from robots. This is not ideal but I could not figure how to redirect [forum.example.com...] to http://example.com/portal.php?*sameanythinghere*

jdMorgan

1:58 pm on May 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The value of a clear question cannot be under-estimated:

RewriteCond %{HTTP_HOST} ^forum\.example\.com
RewriteRule ^portal\.php$ http://example.com/portal.php [R=301,L]

As described above, the query string (if any) is passed through this rule without any change.

Jim

[edited by: jdMorgan at 12:28 am (utc) on May 23, 2009]