Forum Moderators: phranque

Message Too Old, No Replies

Need help with htaccess redirect.

         

Shaneee

1:05 pm on May 4, 2020 (gmt 0)

5+ Year Member



Hello. So I've changed my forum software and I've kept a copy of the old version which is still accessible by directing to the folder and the new version is on the subdomain.
How can I redirect old links for the subdomain that were linking to a viewtopic.php file to the direct folder now?

For example https://forum.example.co.uk/viewtopic.php?f=4 redirecting to https://example.co.uk/forum/viewtopic.php?f=4

The new forum uses index.php to link topics/forums so a wildcard redirect on viewtopic.php would be ideal.

Thanks.


[edited by: not2easy at 1:21 pm (utc) on May 4, 2020]
[edit reason] Please use example for domains/readability [/edit]

w3dk

1:46 pm on May 4, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



Using mod_rewrite in the .htaccess file at the root of the subdomain:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(forum)\.(example\.co\.uk) [NC]
RewriteRule ^viewtopic\.php$ https://%2/%1/$0 [R=302,L]


The query string is passed through unaltered. The $0 backreference contains the entire match from the RewriteRule pattern (ie. "viewtopic.php"). The %1 and %2 backreferences simply contain the subdirectory and domain to redirect to (saves repetition) - captured from the preceding condition.

If the subdomain and main domain point to different places then you don't need the additional condition. For example:

RewriteRule ^viewtopic\.php$ https://example.co.uk/forum/$0 [R=302,L]


Test with a 302 (temporary) redirect and only change to 301 (permanent) - if that is the intention - once you have confirmed that everything works OK. This is to avoid any erroneous caching issues.

[edited by: w3dk at 1:51 pm (utc) on May 4, 2020]

not2easy

1:47 pm on May 4, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi Shaneee and welcome to WebmasterWorld [webmasterworld.com]

Sorry for the edits but as the Apache forum [webmasterworld.com] requests, using "example" in place of other domain name substitutes will leave your intent clear for others to see.

What have you tried so far and what results did you have? If you haven't tried anything yet, you could use our site search to find similar cases and try them. Maybe search for 'redirect to subdomain' to start?

We like to help when things aren't right but we don't offer copy/paste answers because they can cause more problems than they resolve. Even pasting a correct "answer" in the wrong place can set off a chain of problems. We prefer to help you understand your code, as explained in the Apache Charter [webmasterworld.com]

It isn't an instant fix, but it is more useful ;)

Feel free to use w3dk's suggestion.

lucy24

3:24 pm on May 4, 2020 (gmt 0)

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



https://forum.example.co.uk/viewtopic.php?f=4
redirecting to
https://example.co.uk/forum/viewtopic.php?f=4
If the old URL starts with
^viewtopic
while the new one doesn’t, then I don’t see any need to look at the HTTP_HOST at all.

The new forum uses index.php to link topics/forums
I hope you didn’t mean that “index.php” is part of the visible URL. From the example given, it doesn’t seem to be.

w3dk

3:58 pm on May 4, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



If the old URL starts with
^viewtopic
while the new one doesn’t, then I don’t see any need to look at the HTTP_HOST at all.


True, true.

Shaneee

11:32 pm on May 4, 2020 (gmt 0)

5+ Year Member



not2easy that's fine sorry about that.

Let me try explain this better. I'm not too sure if the host has mod_rewrite enabled.

I'm looking for a simple way of redirecting https://forum.example.co.uk/viewtopic.php?f=4 to https://example.co.uk/forum/viewtopic.php?f=4 while not afecting the current link structure of the new forum which uses https://forum.example.co.uk//index.php?threads/example-thread

lucy24

11:49 pm on May 4, 2020 (gmt 0)

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



I'm not too sure if the host has mod_rewrite enabled
Perhaps you should go find out before asking questions whose answers may prove to be of no use to you. But since you refer to index.php, I think it is wildly improbable that you would not have access to mod_rewrite.

The redirect can also be done in mod_alias, but it’s better to do all your redirects within the same module. Regardless of module, the whole query string will be reappended by default, so you need not be concerned about that.

phranque

2:07 am on May 5, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, Shaneee!

I'm looking for a simple way of redirecting https://forum.example.co.uk/viewtopic.php?f=4 to https://example.co.uk/forum/viewtopic.php?f=4 while not afecting the current link structure of the new forum which uses https://forum.example.co.uk//index.php?threads/example-thread


i would use this in the .htaccess file of the document root directory for forum.example.co.uk:
RewriteRule ^viewtopic\.php$ https://example.co.uk/forum/viewtopic.php [R=301,L]

lucy24

2:45 am on May 5, 2020 (gmt 0)

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



the document root directory for forum.example.co.uk
Ouch, good point. The redirects--regardless of what module issues them--have to be located in the directory where the old files lived, or else the requests will never see the redirect.

It makes absolutely no difference whether those files physically still exist, so long as the directory is reachable. This means, paradoxically, that you have to keep a subdomain active on the server even if you're entirely shutting it down and all requests for it are to be redirected, so its directory might have no content except an htaccess file and a robots.txt. (Basically the same as if you're giving up a domain and redirecting everything to some other domain.)